sed/awk to insert comment at defined line number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed/awk to insert comment at defined line number
# 1  
Old 03-11-2003
Question 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 ? Smilie

Thanks,
homefp
# 2  
Old 03-11-2003
I believe if you read this post you will have the answer you need.
# 3  
Old 03-11-2003
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}/;}'
# 4  
Old 03-14-2003
Thanks a lot !!! Smilie
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
# 5  
Old 03-14-2003
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 Smilie
# 6  
Old 03-14-2003
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
# 7  
Old 03-17-2003
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 ? Smilie

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.Smilie

Thanks,
homefp
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

selection particular number of line from a bunch by user defined limits.

hello i am having a file having a matrix as the following 4.1 5.5 6.55 7.2 8.2 1.002 i am having around 1 lakh rows, now i need a program in which i show give min x and min y and min z values and as well as max x max y max z, the values between these minimun and maximum values should be... (1 Reply)
Discussion started by: charan pattabhi
1 Replies

2. Shell Programming and Scripting

Insert a value in a pipe delimited line (unsig sed,awk)

Hi, I want to insert a value (x) in the 3rd position of each line in a file like below a|b|c|d|1 a|b|c|d a|b|c|d|e|1 a|b|cso that output file looks like a|b|x|c|d|1 a|b|x|c|d a|b|x|c|d|e|1 a|b|x|cI can do that using perl as below #!/usr/bin/perl -w use strict; #inserting x at... (5 Replies)
Discussion started by: sam05121988
5 Replies

3. Shell Programming and Scripting

sed and awk to insert a line after a para

hi I am having a file like this ############################## mod1 ( a(ll) , b( c), try(o) , oll(ll) go(oo) , al(ll) mm(al) , lpo(kka) kka(oop) ); mod2 ( jj(ll) , c( kk), try1q(o1) , ofll(lll) gao(oo1) , ala(llaa) mmf(adl) , lddpo(kkad) kkda(oodp) );... (20 Replies)
Discussion started by: kshitij
20 Replies

4. Shell Programming and Scripting

Help on Sed/awk/getting line number from file

I Have file1 with below lines : #HostNameSelection=0 :NotUsed #HostNameSelection=1 :Automatic #HostNameSelection=3 :NotForced I have file2 which has similar lines but with different values I want to copy the changes from file1 to file2 ,line by line only if line begins with '#'. for... (7 Replies)
Discussion started by: mvr
7 Replies

5. Shell Programming and Scripting

Awk to extract lines with a defined number of characters

This is my problem, my file (file A) contains the following information: Now, I would like to create a file (file B) containing only the lines with 10 or more characters but less than 20 with their corresponding ID: Then, I need to compare the entries and determine their frequency. Thus, I... (7 Replies)
Discussion started by: Xterra
7 Replies

6. Shell Programming and Scripting

Comment a line with SED

I have around 25 hosts and each hosts has 4 instance of jboss and 4 different ip attached to it . I need to make some changes to the startup scripts. Any tips appreciated. I have total of 100 instances which bind to 100 different ip address based on instance name. For example File1 ... (1 Reply)
Discussion started by: gubbu
1 Replies

7. Shell Programming and Scripting

Using sed to comment out line in /etc/vfstab

I am running a script remotely to do the following 1. Kill all processes by a user 2. Uninstall certain packages 3. FTP over a new file 4. Kill a ldap process that is not allowing my /devdsk/c0t0d0s7 slice to un-mount 5. Unmount /h 6. comment out the slice in vfstab 7. newfs the... (9 Replies)
Discussion started by: deaconf19
9 Replies

8. UNIX for Dummies Questions & Answers

Using awk to get a line number to delete, piping through sed

Alright, I'm sure there's a more efficient way to do this... I'm not an expert by any means. What I'm trying to do is search a file for lines that match the two input words (first name, last name) in order to remove that line. The removal part is what I'm struggling with. Here is my code: echo... (4 Replies)
Discussion started by: lazypeterson
4 Replies

9. Shell Programming and Scripting

awk or sed for finding closest pattern to a line number

hi guys, I want to do pattern matching with awk or sed but I don't know how. here's what I want: I have a line number for a pattern that I have already found using grep, and I know a pattern like "---" that happens a few lines above that certain line number. I want to print out the chunk... (1 Reply)
Discussion started by: alirezan
1 Replies

10. Shell Programming and Scripting

sed to have defined positionning on line

Hi, I'd like to know how to use sed to position my output to a defined location on a line. Here is my example: A: filename.txt => line x : comment fromA B: very longfilename.txt => line xyzabd : commentfromB and I'd like to have as output from sed : A: filename.txt................=> ... (10 Replies)
Discussion started by: homefp
10 Replies
Login or Register to Ask a Question