Commenting a line matched with a specific string in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Commenting a line matched with a specific string in a file
# 1  
Old 03-27-2014
Tools 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

Last edited by raosr020; 03-27-2014 at 02:09 PM..
# 2  
Old 03-27-2014
Code:
sed -ie "s/\(.*sreenivas.*\)/#\1/" <filename>

# 3  
Old 03-27-2014
Here is an awk solution:

Code:
$cat file.txt
this line gets no comment
comment a line that matched a string "sreenivas" in a file without opening it.
this line gets no comment

Code:
 
$awk '{ if ($0 ~ /sreenivas/) {print "#" $0} else {print $0}}' file.txt
this line gets no comment
#comment a line that matched a string "sreenivas" in a file without opening it.
this line gets no comment

# 4  
Old 03-27-2014
Quote:
Originally Posted by raosr020
I would like to comment a line that matched a string "sreenivas" in a file without opening it.
This won't work, because to find out if there is such line you will definitely have to open it somehow.

If you mean "without interactively opening" and assuming you use "#" as a comment sign (beware - there are some formats with other comment signs), try:

Code:
sed '/sreenivas/ s/^/# /' /path/to/input > /path/to/output

I hope this helps.

bakunin
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep string in a file and paste next line in a specific way

Hello, I know there are many questions and replies regarding grep command. What I would like to do is a bit different. File A: hello world welcome to my page this is my test site how are you I am fine, thank you where have you been I was in hospital really hope you are fine now Thanks,... (10 Replies)
Discussion started by: baris35
10 Replies

2. Shell Programming and Scripting

awk - how to print specific field if a string is matched

hi gurus, I would like to be able to use awk to process 1 file as such: abc 1 2 3 4 5 6 7 8 9 10 flags 1 2 4 flags 1 2 5 abc 2 3 4 5 6 7 8 9 10 11 flags 1 2 3 abc 4 5 6 7 8 9 6 7 78 89 flags 1 2 3 flags 1 2 4 flags 1 2 3 4 I would like to be able to print field 1 and 5 when the... (4 Replies)
Discussion started by: revaroo
4 Replies

3. Shell Programming and Scripting

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

Hello All, I have following file contents 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 line=eeeeee and insert a new line... (19 Replies)
Discussion started by: manishdivs
19 Replies

4. Shell Programming and Scripting

Print only matched string instead of entire line

Hi, I have a file whose lines are something like Tchampionspsq^@~^@^^^A^@^@^@^A^A^Aÿð^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^?ð^@^@^@^@^@^@^@?ð^@^@^@^@^@^@pppsq^@~^@#@^@^@^@^@^@^Hw^H^@^@^@^K^@^@^@^@xp^At^@^FTtime2psq^@ ~^@^^^A^@^@^@^B^A I need to extract all words matching T*psq from the file. Thing is... (4 Replies)
Discussion started by: shekhar2010us
4 Replies

5. Shell Programming and Scripting

Help required on joining one line above & below to the pattern matched string line.

Hi Experts, Help needed on joining one line above & below to the pattern matched string line. The input file, required output is mentioned below Input file ABCD DEFG5 42.0.1-63.38.31 KKKK iokl IP Connection Available ABCD DEFG5 42.0.1-63.38.31 ... (7 Replies)
Discussion started by: krao
7 Replies

6. UNIX for Advanced & Expert Users

capture data from matched string/line

Hi, I have a query as follows : suppose I am matching a string in a file say "start from here" and I want to pick up 'n' number of lines () from the matched string. Is there any way to do that ? 1) going forward I want to do this for every match for the above string 2) or limit this to... (2 Replies)
Discussion started by: sumoka
2 Replies

7. Shell Programming and Scripting

AWK Print Line If Specific Character Is Matched

Hello, I have a file as such: FFFFFFF6C000000 225280 225240 - - rwxs- FFFFFFFF79C00000 3240 3240 - - rwxs- FFFFFFFF7A000000 4096 4096 - - rwxs- FFFFFFFF7A400000 64 64 ... (3 Replies)
Discussion started by: PointyWombat
3 Replies

8. Shell Programming and Scripting

save every line in log file with matched string

i have been doing this script to match every line in a current log file (access_log) with strings that i list from a path (consist of 100 of user's name ex: meggae ).. and then make a directory of every string from the text file (/path/meggae/) --->if it matched.. then print every line from the... (3 Replies)
Discussion started by: meggae
3 Replies

9. UNIX for Advanced & Expert Users

Command to display nth line before the string is matched.

All, Is there any way out to display the nth line before the string is matched ??? Eg : If i have a file which has the following contents and if i want to get the 3rd line before the string is matched a b c d e f if i give the input as f and lines before the match as 3 then it should... (5 Replies)
Discussion started by: helper
5 Replies

10. Shell Programming and Scripting

Commenting a Line In a File

HI all I am working on a script, few details are as follows. I have one properties File and one script. The property file contains the JOBID which are to be executed and the Script runs these jobs ONE by ONE. After completing the JOB I need to comment that job in the property File. This is the... (3 Replies)
Discussion started by: Prashantckc
3 Replies
Login or Register to Ask a Question