Confused by the sed command

Hi Gurus,

I am a little bit confused by the sed command.

my file is below

 
cat sample
aaa
bbb
ccc
ddd
eee
fff
ggg
hhh
iii

below is command and output.

 
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.

Please correct me if anything incorrect.

sed -n may be your friend. Thus don't print unless you explicitly say so.

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 know i need to use N as an option to SED.

Appreciate the simple example and answer. Thanks

@lmre: the n command seems more appropriate here.
To delete an empty line following a line ending with a "

sed '/"$/{n;/^$/d;}' file
1 Like

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.

23445w34,"djhahfhake,shdjgekagdgejs"$
23445w34,"djhahfhake","shdjgekagdgejs"$
23445w34,"kjshdfd","shdjg$
"."ekagdgejs"$
23445w34,"djhamdhzkjfsdtheree,jgekagdgejs"$
23445w34,"doubletroubkeinhungary","shdjgekagdgejs"$

Line 3 is the broken line and ends with $ and i want to APPEND line 4 to line 3.
Thats it.

I have used GAWK and AWK like this :
rskjhbvsfs1:icsoka:~/code> awk '{if ( /.$/ != /\"$/ ) printf $0 ; else print $0 } southafrica.txt

Now i want to win using SED.

Ah, then it's

sed '/"$/!{N;s/\n//;}' file

If not ending with a " append the next line, delete the embedded \n

---------- Post updated at 05:20 PM ---------- Previous update was at 03:18 PM ----------

The awk command is still easier here, an improved version is

awk '{if ( /"$/ ) print $0 ; else printf "%s",$0 }' file

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:

sed '                                                       
:loop
/"$/b
$!N;s/\n//
t loop
' file

or, using the hold space:

sed '
H;/"$/!{$!d;}
s/.*//;x;s/\n//g
' file
1 Like