I would like to repeat each line's content of a file, any quick solution using sed.
supposed the input file is
abc def 123
The expected ouput is:
abcabc defdef 123123
sed 's \(.*\) \1\1 ' infile
This might work for you:
echo -e 'aaA\nbbB\nccC' | sed 's/.*/&&/'
aaAaaA
bbBbbB
ccCccC
sed 'h;G;s/\n//' file.txt
It's even simpler if you take advantage of the variable \0, which holds the string that was matched:
sed 's/.*/\0\0/'
Try this
awk '{print $0$0}' temp.txt
Related
I would like to insert a space, before the 3rd last character of each line, to turn this:
CC287999221
CHGFFDTTT34AAA387
CH654AZ0987XX277
Into this:
CC287999 221
CHGFFDTTT34AAA 387
CH654AZ0987XX 277
So far I've tried:
sed -i 's/.*\(...\)/ \1/' file
However this remove the preceding text also.
Thank you
One way:
sed 's/\(...$\)/ \1/' file
Just match the last 3 characters, while substituting put a space and then the matched pattern(\1)
With awk could you please try following.
awk '{print substr($0,1,length($0)-3),substr($0,length($0)-2)}' Input_file
tried on gnu sed:
sed -E 's/\S{3}\s*$/ &/' file
Another awk proposal:
awk '{sub(/.{3}$/," &")}1' file
CC287999 221
CHGFFDTTT34AAA 387
CH654AZ0987XX 277
For example:
Event .123232434
1232323 Event
1233 Event 234
Event 2323
I just want to the rows start with "Event" through awk
is there some way?
It's just:
/^Event/
A rule that isn't associated with any code prints every line that matches.
As paxdiablo pointed out, you could also use grep in this case:
grep '^Event'
If you want to read the input from a file, this becomes
awk '/^Event/' /path/to/file
or
grep '^Event' /path/to/file
You don't need awk for this, you can just use:
grep '^[^0-9]' inputfilename
That will give you all lines that start with a character that isn't a digit.
If you must use awk, it has an equivalent variant:
awk '$0 ~ /^[^0-9]/' inputfilename
I have a file with pipe-separated fields. I want to print a subset of field 1 and all of field 2:
cat tmpfile.txt
# 10 chars.|variable length num|text
ABCDEFGHIJ|99|U|HOMEWORK
JIDVESDFXW|8|C|CHORES
DDFEXFEWEW|73|B|AFTER-HOURS
I'd like the output to look like this:
# 6 chars.|variable length num
ABCDEF|99
JIDVES|8
DDFEXF|73
I know how to get fields 1 & 2:
cat tmpfile.txt | awk '{FS="|"} {print $1"|"$2}'
And know how to get the first 6 characters of field 1:
cat tmpfile.txt | cut -c 1-6
I know this is fairly simple, but I can't figure out is how to combine the awk and cut commands.
Any suggestions would be greatly appreciated.
You could use awk. Use the substr() function to trim the first field:
awk -F'|' '{print substr($1,1,6),$2}' OFS='|' inputfile
For your input, it'd produce:
ABCDEF|99
JIDVES|8
DDFEXF|73
Using sed, you could say:
sed -r 's/^(.{6})[^|]*([|][^|]*).*/\1\2/' inputfile
to produce the same output.
You could use cut and paste, but then you have to read the file twice, which is a big deal if the file is very large:
paste -d '|' <(cut -c 1-6 tmpfile.txt ) <(cut -d '|' -f2 tmpfile.txt )
Just for another variation: awk -F\| -vOFS=\| '{print $1,$2}' t.in | cut -c 1-6,11-
Also, as tripleee points out, two cuts can do this too: cut -c 1-6,11- t.in | cut -d\| -f 1,2
I like a combination of cut and sed, but that's just a preference:
cut -f1-2 -d"|" tmpfile.txt|sed 's/\([A-Z]\{6\}\)[A-Z]\{4\}/\1/g'
Result:
# 10-digits|variable length num
ABCDEF|99
JIDVES|8
DDFEXF|73
Edit: (Removed the useless cat) Thanks!
I'm trying to write a sed command to convert lines:
<http://dbpedia.org/resource/BoA> <http://dbpedia.org/ontology/wikiPageWikiLink> <http://dbpedia.org/resource/Ne-Yo> .
<http://dbpedia.org/resource/BoA> <http://dbpedia.org/ontology/wikiPageWikiLink> <http://dbpedia.org/resource/Tablo> .
to
BoA, Ne-Yo
BoA, Tablo
I know how to match and print using /(/) but I can't find a way to print two matches.
Using awk you can do:
awk -F"[/>]" '/http/ {print $5 ", " $15}' file
BoA, Ne-Yo
BoA, Tablo
Use parentheses and then \1 to print the first match, \2 to print the second match, and so on.
sed 's|<http://dbpedia.org/resource/\([^>]\+\)> <[^>]\+> <http://dbpedia.org/resource/\([^>]\+\)>.*|\1,\2|g' input.txt
A little verbose, though. Put your text into input.txt file.
Less verbose, but also less accurate than #rendon's solution:
sed -e 's?.*/resource/\([^>]*\)>.*/resource/\([^>]*\).*?\1, \2?' input.txt
If it's good enough then this is more readable.
This might work for you (GNU sed):
sed -r 's|[^>]*/([^>]*)>.*/([^>]*).*|\1, \2|' file
I tried the '/pat1/,/pat2/p', but I want to print only the text between the patterns, not the whole line. How do I do that?
A pattern range is for multiline patterns. This is how you'd do that:
sed -n '/pat1/,/pat2/{/pat1\|pat2/!p}' inputfile
-n - don't print by default
/pat1/,/pat2/ - within the two patterns inclusive
/pat1\|pat2/!p - print everything that's not one of the patterns
What you may be asking for is what's between two patterns on the same line. One of the other answers will do that.
Edit:
A couple of examples:
$ cat file1
aaaa bbbb cccc
123 start 456
this is what
I want
789 end 000
xxxx yyyy zzzz
$ sed -n '/start/,/end/{/start\|end/!p}' file1
this is what
I want
You can shorten it by telling sed to use the most recent pattern again (//):
$ sed -n '/.*start.*/,/^[0-9]\{3\} end 0*$/{//!p}' file1
this is what
I want
As you can see, I didn't have to duplicate the long, complicated regex in the second part of the command.
sed -r 's/pat1(.*)pat2/\1/g' somefile.txt
I don't know the kind of pattern you used, but i think it is also possible with regular expressions.
cat myfile | sed -r 's/^(.*)pat1(.*)pat2(.*)$/\2/g'
you can use awk.
$ cat file
other TEXT
pat1 text i want pat2
pat1 TEXT I
WANT
pat2
other text
$ awk -vRS="pat2" 'RT{gsub(/.*pat1/,"");print}' file
text i want
TEXT I
WANT
The solution works for patterns that span multiple lines