Commenting a specific line and inserting a new line after commented line.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Commenting a specific line and inserting a new line after commented line.
# 8  
Old 12-17-2012
I left out a trailing slash. Try:
Code:
sed 's/^line=.*/#&\
line=ffffff/' file

--edit--
Don Cragun beat me to it Smilie

Last edited by Scrutinizer; 12-17-2012 at 05:28 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 9  
Old 12-17-2012
Quote:
Originally Posted by Scrutinizer
Try:
Code:
sed 's/^line=.*/#&\
line=ffffff/' file

Hello,

Once I insert a new line then I would like to put some comment in front of that line.

I am able to achieve this using following separate command:
Code:
sed 's/\(^line=.*\)/\1 #Code Change Comment/' manish

But I wonder if I can achieve this within previous command. I did try options but not succeeded.

Please help me if there is any way to get this.

Thanks.
# 10  
Old 12-17-2012
Hi, I am not sure what you mean, one of these?

Code:
$ sed 's/^line=.*/#&\
line=ffffff    # code change/' file
#line=aaaaaa
#line=bbbbbb
#line=cccccc
#line=dddddd
#line=eeeeee
line=ffffff    # code change
#comment=11111
#comment=22222
#comment=33333
#comment=44444
comment=55555

Code:
$ sed 's/^line=.*/#&   #code change\
line=ffffff/' file
#line=aaaaaa
#line=bbbbbb
#line=cccccc
#line=dddddd
#line=eeeeee   #code change
line=ffffff
#comment=11111
#comment=22222
#comment=33333
#comment=44444
comment=55555

# 11  
Old 12-17-2012
Yes. Got it!! Thanks.

Code:
sed 's/^line=.*/#&\
line=$restoreufs   $comment/' manish

where
Code:
restoreufs="line=ffffff"
comment="This comment line"

When I am trying to read restoreufs & comment variable values then it is not able to expand $restoreufs and $comment values
Output:
Code:
#line=eeeeee
line=$restoreufs   $comment
#comment=11111

I tried to use "double cotes" and "eval" then instead of inserting a new line it is appending data in existing line

Code:
sed "s/^line=.*/#&\
line=$restoreufs   $comment/" manish

OR
Code:
eval "sed 's/^line=.*/#&\
line=$restoreufs   $comment/' manish"

Output:
Code:
#line=dddddd
#line=eeeeeeline=line=ffffff   This comment line
#comment=11111

Que: How to display the variable values correctly in new line.

My actual command in script is:
Code:
sed 's/^restoredb2=.*/#&\
restoredb2=$restoredb2_value   # $comment/' /home/script/restore_$(basename $PWD).sh


Last edited by manishdivs; 12-17-2012 at 08:21 PM..
# 12  
Old 12-17-2012
Try combining single quotes and double quotes, for example:
Code:
sed 's/^line=.*/#&\
'"$restoreufs    # $comment/" file



or use an extra escape for the escape character itself:
Code:
sed "s/^line=.*/#&\\
$restoreufs    # $comment/" file

# 13  
Old 12-17-2012
Try it like this:
Code:
restoreufs="line=ffffff"
comment="This comment line"

sed "s/^line=.*/#&\
\n${restoreufs}   ${comment}/" file

$ test.sh
#line=aaaaaa
#line=bbbbbb
#line=cccccc
#line=dddddd
#line=eeeeee
line=ffffff   This comment line
#comment=11111
#comment=22222
#comment=33333
#comment=44444
comment=55555

# 14  
Old 12-18-2012
Code:
sed "s/^restoredb2=.*/#&\\
restoredb2=$restoredb2_value   # $comment/" restore_$(basename $PWD).sh

I am using above command to look for a line starting with word "restoredb2=" and commenting it. Also, I am inserting new data line in the immediate next line.

Issue: When I am running above command then it is not able to read a line starting with space and followed by my search string.

For example::
Able to read:
Code:
restoredb2=testing12345

But not able to read
Code:
  restoredb2=testing12345 (Please note initial 2 spaces)


Please help me to modify command to work similarly in case it find search string directly or find it with leading spaces.

Thank you.

Last edited by manishdivs; 12-19-2012 at 02:44 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. UNIX for Dummies Questions & Answers

Commenting a line matched with a specific string in a file

Hi, I would like to comment a line that matched a string "sreenivas" in a file without opening it. Thanks in advance. Regards, Sreenivas (3 Replies)
Discussion started by: raosr020
3 Replies

3. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

4. Shell Programming and Scripting

Using awk to read a specific line and a specific field on that line.

Say the input was as follows: Brat 20 x 1000 32rf Pour 15 p 1621 05pr Dart 10 z 1111 22xx My program prompts for an input, what I want is to use the input to locate a specific field. Like if I type in, "Pou" then it would return "Pour" and just "Pour" I currently have this line but it is... (6 Replies)
Discussion started by: Bungkai
6 Replies

5. Shell Programming and Scripting

Counting rows line by line from a specific column using Awk

Dear UNIX community, I would like to to count characters from a specific row and have them displayed line-by-line. I have a file called testAwk2.csv which contain the following data: rabbit penguin goat giraffe emu ostrich I would like to count in the middle row individually... (4 Replies)
Discussion started by: vnayak
4 Replies

6. Shell Programming and Scripting

script for inserting line at specific place in file

I use zentyal for my server admin, which is great but zentyal auto-generates config file on boot and hence overwrites any changes made directly to config files. In order to allow multiple user access to a MS ACCESS database, I need to customise the smb.conf file and add the following line to the... (9 Replies)
Discussion started by: barrydocks
9 Replies

7. Shell Programming and Scripting

Inserting a line in a file after every alternate line

Friends , I have a large file and i need to insert a line after every line.I am actually unaware how to do it.Any help appreciated. My File control station *ATM* , qread $OSS.Jul13A.FI01 interval 1 intcount 1 control station *ATM* , qread $OSS.Jul13A.FI02 interval 1 intcount... (4 Replies)
Discussion started by: appu2176
4 Replies

8. Shell Programming and Scripting

Problem inserting text into file after specific line

this is utterly embarassing :( after posting here i revisited my files and found that when i used "vi" instead of a gui based editor, i suddenly found that the indentations were in fact wrong :( sorry about this :( (0 Replies)
Discussion started by: mocca
0 Replies

9. UNIX for Advanced & Expert Users

Inserting a line before the line which matches the patter

Hi Is there any command where we can insert a line "2|||" before every line starting with "3|" my input is as follows 1|ETG|12345 3|79.58|||GBP|| 1|ETG|12345 3|79.58|||GBP|| 1|ETG|12345 2|EN_GB||Electrogalvanize 0.5 m2 ( Renault ) 1|ETG|12345 3|88.51|||GBP|| desired output... (10 Replies)
Discussion started by: laxmi131
10 Replies

10. Shell Programming and Scripting

inserting a line after a perticular line

Hi, Suppose I have a file which contains many lines, ---------------------- wake up study hard play a little getting hungry <---- insert a line after this line watch TV go shopping come back home getting hungry <--- insert a line after this line listen music surf internet go to... (2 Replies)
Discussion started by: uday610
2 Replies
Login or Register to Ask a Question