Sponsored Content
Top Forums Shell Programming and Scripting Commenting a specific line and inserting a new line after commented line. Post 302745549 by manishdivs on Monday 17th of December 2012 01:36:52 PM
Old 12-17-2012
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
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
PPI::Token::Comment(3)					User Contributed Perl Documentation				    PPI::Token::Comment(3)

NAME
PPI::Token::Comment - A comment in Perl source code INHERITANCE
PPI::Token::Comment isa PPI::Token isa PPI::Element SYNOPSIS
# This is a PPI::Token::Comment print "Hello World!"; # So it this $string =~ s/ foo # This, unfortunately, is not :( bar /w; DESCRIPTION
In PPI, comments are represented by "PPI::Token::Comment" objects. These come in two flavours, line comment and inline comments. A "line comment" is a comment that stands on its own line. These comments hold their own newline and whitespace (both leading and trailing) as part of the one "PPI::Token::Comment" object. An inline comment is a comment that appears after some code, and continues to the end of the line. This does not include whitespace, and the terminating newlines is considered a separate PPI::Token::Whitespace token. This is largely a convenience, simplifying a lot of normal code relating to the common things people do with comments. Most commonly, it means when you "prune" or "delete" a comment, a line comment disappears taking the entire line with it, and an inline comment is removed from the inside of the line, allowing the newline to drop back onto the end of the code, as you would expect. It also means you can move comments around in blocks much more easily. For now, this is a suitably handy way to do things. However, I do reserve the right to change my mind on this one if it gets dangerously anachronistic somewhere down the line. METHODS
Only very limited methods are available, beyond those provided by our parent PPI::Token and PPI::Element classes. line The "line" accessor returns true if the "PPI::Token::Comment" is a line comment, or false if it is an inline comment. SUPPORT
See the support section in the main module. AUTHOR
Adam Kennedy <adamk@cpan.org> COPYRIGHT
Copyright 2001 - 2011 Adam Kennedy. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of the license can be found in the LICENSE file included with this module. perl v5.16.2 2011-02-25 PPI::Token::Comment(3)
All times are GMT -4. The time now is 08:09 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy