Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-17-2012
Registered User
 
Join Date: Jul 2010
Location: Ghaziabad, India
Posts: 57
Thanks: 11
Thanked 1 Time in 1 Post
Tools Commenting a specific line and inserting a new line after commented line.

Hello All,

I have following file contents

Code:
cat file
#line=aaaaaa
#line=bbbbbb
#line=cccccc
#line=dddddd
line=eeeeee
#comment=11111
#comment=22222
#comment=33333
#comment=44444
comment=55555
 
Testing script
Good Luck!

I would like to comment line
Code:
line=eeeeee

and insert a new line
Code:
line=ffffff

immediate after this.

I wrote following command to comment my line:

Code:
sed 's/\(^line=*\)/#\1/' manish

And Google for inserting a new line and found following solution:

Code:
sed '/^line=/ a\
line=ffffff' manish

  1. I tried to combine both commands in a single command using “|” but I am not able to get it correctly. Can anyone please help me to achieve this objective?
  2. 2nd command to insert a new line is divided in 2 lines (using “\”). I tried several option to make it in a single line but every time it gives me some error. Can you please help me to combine 2nd command in a single line?


Output should look like:

Code:
cat file
#line=aaaaaa
#line=bbbbbb
#line=cccccc
#line=dddddd
#line=eeeeee
line=ffffff
#comment=11111
#comment=22222
#comment=33333
#comment=44444
comment=55555
 
Testing script
Good Luck!


Thank you.
Manish
Sponsored Links
    #2  
Old 12-17-2012
bartus11's Avatar
Registered User
 
Join Date: Apr 2009
Posts: 3,146
Thanks: 3
Thanked 957 Times in 936 Posts
Try:
Code:
sed 's/\(^line=.*\)/#\1\nline=ffffff/' manish

Sponsored Links
    #3  
Old 12-17-2012
Registered User
 
Join Date: Jul 2010
Location: Ghaziabad, India
Posts: 57
Thanks: 11
Thanked 1 Time in 1 Post
bartus11,
Many thanks for your reply.

It commented the required line but inserted the line in the commented line not in next line.


Code:
sed 's/\(^line=.*\)/#\1\nline=ffffff/' manish
#line=aaaaaa
#line=bbbbbb
#line=cccccc
#line=dddddd
#line=eeeeeenline=ffffff
#comment=11111
#comment=22222
#comment=33333
#comment=44444
comment=55555
 
Testing script
Good Luck!

My requirment is to get line as:

Code:
#line=eeeeee
line=ffffff

    #4  
Old 12-17-2012
bartus11's Avatar
Registered User
 
Join Date: Apr 2009
Posts: 3,146
Thanks: 3
Thanked 957 Times in 936 Posts
Try this:
Code:
perl -pe 's/^(line=.*)/#$1\nline=ffffff/' manish

The Following User Says Thank You to bartus11 For This Useful Post:
manishdivs (12-17-2012)
Sponsored Links
    #5  
Old 12-17-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,352
Thanks: 144
Thanked 1,756 Times in 1,593 Posts
\n is not a standard sed feature in the replacement part of the substitute command. Try:

Code:
sed 's/^line=.*/#&\
line=ffffff/' file


Last edited by Scrutinizer; 12-17-2012 at 03:56 PM.. Reason: added trailing /
Sponsored Links
    #6  
Old 12-17-2012
Registered User
 
Join Date: Jul 2010
Location: Ghaziabad, India
Posts: 57
Thanks: 11
Thanked 1 Time in 1 Post
Quote:
Originally Posted by Scrutinizer View Post
\n is not a standard sed feature in the replacement part of the substitute command. Try:

Code:
sed 's/^line=.*/#&\
line=ffffff' file

It is giving following error:

Code:
sed: 0602-404 Function s/^line=.*/#&\
line=ffffff cannot be parsed.

Sponsored Links
    #7  
Old 12-17-2012
Registered User
 
Join Date: Jul 2012
Location: San Jose, CA
Posts: 1,486
Thanks: 62
Thanked 537 Times in 470 Posts
Quote:
Originally Posted by manishdivs View Post
It is giving following error:

Code:
sed: 0602-404 Function s/^line=.*/#&\
line=ffffff cannot be parsed.

Try:
Code:
sed 's/^line=.*/#&\
line=ffffff/' file

The Following User Says Thank You to Don Cragun For This Useful Post:
manishdivs (12-17-2012)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script for inserting line at specific place in file barrydocks Shell Programming and Scripting 9 02-10-2011 05:37 AM
Inserting a line in a file after every alternate line appu2176 Shell Programming and Scripting 4 07-14-2010 10:07 AM
Problem inserting text into file after specific line mocca Shell Programming and Scripting 0 03-15-2009 05:32 PM
Inserting a line before the line which matches the patter laxmi131 UNIX for Advanced & Expert Users 10 12-18-2008 06:31 AM
inserting a line after a perticular line uday610 Shell Programming and Scripting 2 10-19-2008 08:06 AM



All times are GMT -4. The time now is 10:57 AM.