Operating System - HP-UX
1822231 Members
3707 Online
109642 Solutions
New Discussion юеВ

sed command explanation needed

 
subbukns
New Member

sed command explanation needed

Hi,

Could you please explain me the below statement -- phrase wise.

sed -e :a -e '$q;N;'$cnt',$D;ba' abc.txt > xyz.txt


as early as possible.

Thanks,
Subbu
3 REPLIES 3
Yang Qin_1
Honored Contributor

Re: sed command explanation needed

Hi, Subbu,

-e edit according to "operation" like 's/string1/strings2/'

:a label the edit operation as "a" and it will be used in branch option "b" so 'ba' means branch to "a".

The problem here is that whithout knowing what are $q, $cnt and $D it is impossible to tell exactly what this statement trying to do.

Regards,
Yang
spex
Honored Contributor

Re: sed command explanation needed

Hi Subbu,

This script is from sed one-liners:
http://www.student.northpark.edu/pemente/sed/sed1line.txt

It packs a lot of functionality into a very small program. Here's my best shot at parsing it:

sed -e :a -e '$q;N;'$cnt',$D;ba' abc.txt > xyz.txt

-e <script> -- read from <script>
:a -- define label a
$q -- quit at EOF
N -- read in next line
'$cnt' -- $cnt-1 lines will be displayed
$D -- delete at EOF
ba -- branch to label a
abc.txt -- take abc.txt as input
> xyz.txt -- output to xyz.txt

For an easier-to-understand sed script that emulates 'tail', see "Sed - An Introduction":
http://www.grymoire.com/Unix/Sed.html#toc-uh-30

PCS
subbukns
New Member

Re: sed command explanation needed

Hi Yang & PCS,

Thank you very much guys for your quick response.

cheers,
Subbu.