sed '/ddd/{x;p;}' sample.txt
aaa
bbb
ccc
--blank
--blank
eee
fff
ggg
hhh
iii
as per my understanding, {x;p;} when match found, x -- exchange hold place with pattern place which is null with ddd, then p; print, I thought it only print once, but here it prints twice.
Please help me to understand this, thanks in advance.
Ps, I think I figured out this. it works as below. x -- exchange hold place with pattern place which is empty. with ddd then p; print. after finish these two. the default is "print pattern space", so it prints the pattern space content again which is empty.
Actually, in your sample output there are three blank lines that in real life aren't there; it's just two.
And yes, you are right with your analysis - for ddd lines, there is one explicit p rint after the x change, and one implicit. If you want just the explicit one, append (man sed)
This SED command and the MAN pages drive me nuts, its never as simple to follow and even if you use google its almost impossible to get a simple example and visual representation of what you actually need to do. Anybody can read a story , but just show the actual practicality of it, what?
I have a file which has broken lines, unintended line feeds.
I want to repair them, so my thoughts are , find "something" unique about those particular broken lines and then do something to make them append to the rest of the line accordingly.
I have found out how to do this in AWK and GAWK.
I wish to know how to do this using SED which i understand is the most correct method.
I understand i need to use SED '/find something/ {N something something, i dont undersgand the syntax and the material i have referenced doesn't explain this in a one liner so its just ridiculous.
I have tried for a month already on and off but i am not able to win here.
The character which is unique to the broken records is a "$ at the end of the line....which is CORRECT and just a $ character is the incorrect broken line.
So i need to say find lines that end in ONLY $ and then append the NEXT line to that line with the $ character only.
So again, the lines ending with "$ are OK and the lines ending with $ NOT OK.
I use cat -vet filename.txt to see the $ characters.
I need to search for lines ending with $ and not "$
The i want to append the next line to that line,
I dont want to delete anything, i dont know why i need to delete in your example.
There is no empty line here.
sed lacks the capability to print a line without a newline at the end.
So more effort is needed.
Correct implementations with sed are, using a loop and a look-ahead append: