Sed command only works when split up into two steps -


can explain me why combining step 1 , step 2 1 sed command doesn't work:

sed -e :a -e 's/^.\{0,127\}$/& /;ta' \ -e '1,46d' -e '/pharmacom/,+5d' -e 's/^m//g' \ -e ':a;n;$!ba;s/\n//g' -e 's/---*/\n/g' file > result 

but same command split 2 steps works:

step 1:

sed -e :a -e 's/^.\{0,127\}$/& /;ta' -e '1,46d' \ -e '/pharmacom/,+5d' -e 's/^m//g'  file > step 

step 2:

sed -e ':a;n;$!ba;s/\n//g' -e 's/---*/\n/g' step > result 

i first translated commands readable make sense of it:

# pad lines spaces until 128 characters long :a s/^.\{0,127\}$/& / ta  # delete first 46 lines 1,46d  # delete line containing 'pharmacom' , next 5 lines /pharmacom/,+5d  # remove carriage returns s/^m//g  # join rest of lines on single line :a n $!ba s/\n//g  # replace 2 or more dashes newline s/---*/\n/g 

then reduced problematic parts:

# pad lines spaces until 128 characters long :a s/^.\{0,127\}$/& / ta  # join rest of lines on single line :a n $!ba s/\n//g 

or, on single line:

sed ':a;s/^.\{0,127\}$/& /;ta;:a;n;$!ba;s/\n//g' 

the problem use same label name twice, instead of repeating first s command, ta command jumps second label :a, , instead of padding 128 characters, single space inserted.

this fixed using 2 different label names:

sed ':a;s/^.\{0,127\}$/& /;ta;:b;n;$!bb;s/\n//g' 

two remarks:

  • it doesn't matter if use sed -e '...' -e '...' or sed '...;...' in context; both count single command , label names have unique.
  • i'd move d commands beginning of script, or padding work nothing on lines you're deleting anyways.

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo