sed/awk to insert comment at defined line number

Hi there,

may someone easily help me on this :

I want to insert a text in a specific line number like :

linenumb2start=`cat memory_map.dld | nl -ba | egrep -i "label" | cut -f1`

line2insert=`expr $linenumb2start + 2`

and now I need to replace something like {} with {comment} at $line2insert.

The line actually is :
.appdata {}
and no identifier can be added to use it for search and substitution. Of course, appdata and {} are already used as well in several locations. Therefore, I really need to use the line number.

Do you have a solution using sed or awk ? :frowning:

Thanks,
homefp

I believe if you read this post you will have the answer you need.

Actually, sed can locate a string and then go down 2 lines and then make a substitution all by itself....

sed '/label/{n;n;s/{}/{some comment}/;}'

Thanks a lot !!! :slight_smile:
This works perfectly as expected.

Is there as well such a single sed command to remove several lines starting from my "label" line after all ?
May be something like : sed '/label/{d.....n;d...}' < filein > fileout ; mv fileout filein

Thanks again,
homefp

In fact one line command only to replace sequential sed commands as :

sed '/labe/{n;n;d;}' < filein > fileout; mv fileout filein
sed '/label/{n;d;}' < filein > fileout; mv fileout filein
sed '/label/d' < filein > fileout; mv fileout filein

thanks,
homefp :rolleyes:

I may be getting a little lost here. What you really want is to delete some lines?? Not insert some text??

If my current understanding of your request is correct, this should do it...
sed '/label/{N;N;d;}' < input > output

this was just to finalize my need...
I was wondering if in a single sed command it would be possible to remove several lines starting from my label :
the following :
sed '/label/{N;N;d;}' < input > output
is only removing the second line after label, isn't it ?
But if I want to remove in one shot several lines, having only my label as reference, how should I write my sed command ? :confused:

In fact, I would like to replace the 3 lines described in my previous post :
sed '/labe/{n;n;d;}' < filein > fileout; mv fileout filein
sed '/label/{n;d;}' < filein > fileout; mv fileout filein
sed '/label/d' < filein > fileout; mv fileout filein
with only one.:rolleyes:

Thanks,
homefp

No. Give it a try before you reject it. :rolleyes:

Hi,

cat $moduleDepsFile \
| sed -ne "/<!--/ { :c; /-->/! { N; b c; }; /-->/s/<!--.*-->//g }; /^ *$/!p;" > $moduleDepsF
ile.stripped

I am using this command to strip xml comments and it appears to work on linux; i tried to port the same code on solaris and the command fails saying

sed: Label too long: /<\!--/ { :c; /-->/\! { N; b c; }; /-->/s/<\!--.*-->//g }; /^ *$/\!p;

Please help..