I have a file with structure
12312
desc: bleh...
9938
desc: blah...
desc: bloh...
desc: blih...
desc: bluh...
9912
desc: blah...
and i want to move line which matches pattern "desc:" to previous line or delete '\n' in line which goes before every pattern "desc:".
desired output:
12312 desc: bleh...
9938 desc: blah... desc: bloh... desc: blih... desc: bluh...
9912 desc: blah...
I've tried
awk '!/desc:/{
printf "%s ",$0
getline
printf "%s \n",$0
}
/desc/{print}' file
with no result.
actually all the data is the output of awk -F\" '{print $4 "\t" $6}'
maybe i can do something in the first place?
sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed ), sed works by making only one pass over the input(s), and is consequently more efficient.
To Remove the lines from the source file itself, use the -i option with sed command. If you dont wish to delete the lines from the original source file you can redirect the output of the sed command to another file.
Printing Newline in Bash Using the backslash character for newline “\n” is the conventional way. However, it's also possible to denote newlines using the “$” sign.
sed
oneliner
sed ':a $!N;s/\ndesc/ desc/;ta P;D'
Will output
12312 desc: bleh...
9938 desc: blah... desc: bloh... desc: blih... desc: bluh...
9912 desc: blah...
One way with awk
:
$ awk '!/^desc:/&&NR>1{print OFS}{printf "%s ",$0}END{print OFS}' file
12312 desc: bleh...
9938 desc: blah... desc: bloh... desc: blih... desc: bluh...
9912 desc: blah...
Explanation:
!/^desc:/
match lines that don't start with desc:
not including the first line in the file NR>1
.{print OFS}
print an output field separator before the matched line. In awk
the default OFS
is \n
. {printf "%s ",$0}
print every line without a trailing newline. END{print OFS}
after the file has been read add a trailing newline. Live demo: http://ideone.com/ajH14u
$ cat file
12312
desc: bleh...
9938
desc: blah...
desc: bloh...
desc: blih...
desc: bluh...
9912
desc: blah...
$ awk '{printf "%s%s",(/^desc:/?OFS:ors),$0; ors=ORS} END{print ""}' file
12312 desc: bleh...
9938 desc: blah... desc: bloh... desc: blih... desc: bluh...
9912 desc: blah...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With