1830238 Members
3485 Online
109999 Solutions
New Discussion

shell scripts

 
SOLVED
Go to solution
Alice_4
Occasional Advisor

shell scripts

i'm a beginner for sed n awk,so i can't reali understand what those means:

sed 's/'"$1"'/&/g
2,$ d' |
awk ' {
right += length($0);
print right, $0;
}'

help from anyone is highly appreciated...
thank you...
3 REPLIES 3
harry d brown jr
Honored Contributor

Re: shell scripts

Where did you get the code? Do you have any idea what the input is?

It's usually necessary to see the entire picture, not just what's in the box.

live free or die
harry
Live Free or Die
Jean-Louis Phelix
Honored Contributor
Solution

Re: shell scripts

hi,

I try to work without context ...

I can't understand the meaning of first sed line, because basically it's supposed to substitute something by ... the same thing ! The & char is usually used with something else, for example :

sed 's/xxx/< & >/g'

would have changed all occurences of xxx in < xxx > (multiple changes on same lines by adding a final g, else sed go to next line after first replacement.

So, first instruction doesn't seem very useful, try with or without it, it's the same ...

then '2,$ d' would suppress all lines after line 1, so 'line' or 'head -1' would have done it quicker.

then right += length($0); would add length of input record to variable 'right', but in your case, with 1 record only, '=' would have been enough ...

And it finally prints the lenght of the record and the record itself.

If you cat a file into this script, same result would be obtained by :

VAR=$(line)
echo ${#VAR} $VAR

Regards
It works for me (© Bill McNAMARA ...)
Alice_4
Occasional Advisor

Re: shell scripts

hi harry,
i get this code from the net when i'm searching for some notes on sed codes. i can't understand so i post it up.
anyway thanks for your help.
i really have no idea about this code...