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

  Unix/Linux 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 12-17-2012
bartus11's Unix or Linux Image
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 31 March 2015, 4:31 PM EDT
Posts: 3,733
Thanks: 7
Thanked 1,151 Times in 1,121 Posts
Try:
Code:
sed 's/\(^line=.*\)/#\1\nline=ffffff/' manish

Sponsored Links
    #3  
Old Unix and Linux 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 Unix and Linux 12-17-2012
bartus11's Unix or Linux Image
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 31 March 2015, 4:31 PM EDT
Posts: 3,733
Thanks: 7
Thanked 1,151 Times in 1,121 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 Unix and Linux 12-17-2012
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 31 March 2015, 3:32 PM EDT
Location: Amsterdam
Posts: 10,064
Thanks: 324
Thanked 2,631 Times in 2,352 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 Unix and Linux 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 Unix and Linux 12-17-2012
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 1 April 2015, 1:38 AM EDT
Location: San Jose, CA, USA
Posts: 5,958
Thanks: 242
Thanked 1,990 Times in 1,697 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 01:49 AM.