Commenting a specific line and inserting a new line after commented line. | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

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

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-17-2012
manishdivs manishdivs is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 28 May 2014, 8:49 AM EDT
Location: Ghaziabad, India
Posts: 59
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
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 21 October 2014, 2:41 PM EDT
Posts: 3,710
Thanks: 7
Thanked 1,142 Times in 1,113 Posts
Try:
Code:
sed 's/\(^line=.*\)/#\1\nline=ffffff/' manish

Sponsored Links
    #3  
Old 12-17-2012
manishdivs manishdivs is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 28 May 2014, 8:49 AM EDT
Location: Ghaziabad, India
Posts: 59
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
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 21 October 2014, 2:41 PM EDT
Posts: 3,710
Thanks: 7
Thanked 1,142 Times in 1,113 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
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 21 October 2014, 10:43 PM EDT
Location: Amsterdam
Posts: 9,536
Thanks: 284
Thanked 2,420 Times in 2,170 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
manishdivs manishdivs is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 28 May 2014, 8:49 AM EDT
Location: Ghaziabad, India
Posts: 59
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
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 October 2014, 1:38 AM EDT
Location: San Jose, CA, USA
Posts: 4,882
Thanks: 182
Thanked 1,641 Times in 1,392 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
Closed Thread

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 02:24 AM.