bash: merge lines if string is found...
i have a file like this:
Code:
ext1 i need lines starting with "return" to be merged with the previous one: Code:
ext1 return gigi i tried with: Code:
sed -e :a -e '$!N; s/\nreturn/ return/; ta' file.txt thanks! |
solved with:
Code:
awk '!/^return/ { if (line) print line; line = $0; next } { sub(/^[ \t]+/, " "); line = line $0 } END { print line }' file.txt |
Quote:
Code:
sed -e :a -e '$!N; s/\nreturn/ return/;ta' -e 'P;D' $InFile |
Quote:
thanks |
FYI, you can read how to do it with sed here:
http://sed.sourceforge.net/sedfaq4.html#s4.26 As you can see, sed isn't really well-designed for multiple-line editing, particularly when it involves moving "back" in the file. The usual workflow for it is one-way. Incidentally, this can also be done with ed fairly easily. Code:
ed -s file.txt <<HEREDOC Code:
printf '%s\n' 'g/^return/-1 s/$/ /\' '.,+1j' '%p' | ed -s file.txt How to use ed: http://wiki.bash-hackers.org/howto/edit-ed http://snap.nlc.dcccd.edu/learn/nlc/ed.html (also read the info page) |
Ugly solution. REMOVED.
|
Quote:
|
Hi.
That means that the solution I suggested in that post (there were 2 of them actually) turned out to be ugly and useless when I reread the thread once again. There are no simple way to remove wrong post, so I replaced its contents with that phrase. |
All times are GMT -5. The time now is 07:24 AM. |