Add comment if not present


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add comment if not present
# 1  
Old 12-11-2014
Add comment if not present

I have a file

Code:
cat /root/file
#import node1
#import node2
import node2
import node4

After sed/awk operation the file should be as follows

Code:
cat /root/file
#import node1
#import node2
#import node2
#import node4

I was able to retrieve the #import, but not able to place comment in missing lines.
Code:
[root@server node]# cat /root/file  |  egrep  -o "[^ ]+import"
#import
#import

# 2  
Old 12-11-2014
How about
Code:
sed 's/^[^#]/#&/' file
#import node1
#import node2
#import node2
#import node4

# 3  
Old 12-11-2014
Quote:
After sed/awk operation
Providing a (verbose) awk variant...
Code:
awk '$0 !~ /^#/ { $0="#"$0 } { print }' file

# 4  
Old 12-11-2014
Thanks rubic and junior-helper. How can we insert to file using awk, ie similar to sed -i
# 5  
Old 12-11-2014
Hello anil510,

We can use following command for same.
Code:
sed -i 's/^[^#]/#&/'  Input_file

Thanks,
R. Singh
# 6  
Old 12-11-2014
Quote:
Originally Posted by anil510
How can we insert to file using awk, ie similar to sed -i
Unfortunately, awk has no such option. If you want to use the awk approach, you'll need something like
Code:
awk ... file > file.new
mv file.new file

PS: Under the hood, sed does the same Smilie
# 7  
Old 12-11-2014
Code:
$ awk -F"#" ' NF==1 ? $0="#"$0 : 1 ' file
#import node1
#import node2
#import node2
#import node4
$ awk -F"#" ' $2 ? 1 : $0="#"$0 ' file
#import node1
#import node2
#import node2
#import node4
$ awk ' { sub("^[^#]","#&") } 1 ' file
#import node1
#import node2
#import node2
#import node4


Last edited by anbu23; 12-11-2014 at 09:05 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add comment on last line if found match

Hi All, totally new on it , normally use it for just 1 line. i'm looking for help. i'm have 2 file. file 1 : -------------------------------------------------- c12 c1 c3 -------------------------------------------------- file 2: other content ... (10 Replies)
Discussion started by: kttan
10 Replies

2. Shell Programming and Scripting

Search, and add if present

Dear All, I have to find a way to reorganize a table file according to the last column. The input file looks like this: cat Input1.txt: ID:12:23:00Q EU232 2342 234 123 231 aa1;ab2 ID:11:22:00E EU112 1232 211 112 233 ab2;ac3 ID:19:24:00S EU121 569 ... (7 Replies)
Discussion started by: loba
7 Replies

3. Shell Programming and Scripting

Help about comment verification

Hello, I have a file, in which line 40 is commented. It is basically a cron job, #05,35,50 * * * * /usr/local/scripts/my.sh how i can i verify the line 40 is commented, if not then give me message not commented, otherwise provide us message it is commented. (5 Replies)
Discussion started by: learnbash
5 Replies

4. UNIX for Dummies Questions & Answers

Concatenate files and delete source files. Also have to add a comment.

- Concatenate files and delete source files. Also have to add a comment. - I need to concatenate 3 files which have the same characters in the beginning and have to remove those files and add a comment and the end. Example: cat REJ_FILE_ABC.txt REJ_FILE_XYZ.txt REJ_FILE_PQR.txt >... (0 Replies)
Discussion started by: eskay
0 Replies

5. Shell Programming and Scripting

add line and remove comment in some script

Hi, i need some help. i am not sure about my idea. I have a script directory under my home directory,which has a lot of scripts in it. These are some names of the scripts in /axxhome/prdv/script aly300.sh axt300.sh arv300.sh clp300.sh ctth300.sh aly400.sh axt400.sh arv400.sh... (6 Replies)
Discussion started by: debu000
6 Replies

6. Shell Programming and Scripting

how to add more number in comment section D0000,S0000,D0000,D12345,S12345 |

#!/bin/bash repository=$1; txn=$2; #echo -e "\n\npre-hook Script called\n\n"; logmsg=$(svnlook log ${repository} -t ${txn}); logmsg_vals="${logmsg%%|*}|"; logmsg_char="${logmsg_vals:0:1}"; logmsg_len=$(echo ${logmsg} | cut -d' ' -f1); # logmsg_len2=$(echo ${logmsg} | awk '{print... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

7. UNIX for Dummies Questions & Answers

comment lines

Is there a command to put a comment (#) in a whole part of a shell script? (5 Replies)
Discussion started by: vero_81
5 Replies

8. Shell Programming and Scripting

How to copy and comment out

I need help with a script. Here's what I want to do. I have a file "upsrk.txt" which can be either under "/home/upsrk" OR "/home/oracle" On some systems file "upsrk.txt" may not exist, in that case I want to just exit out of the script. If the file is there, I want to check if I have the... (3 Replies)
Discussion started by: upsrk
3 Replies

9. Shell Programming and Scripting

Need to add a comment line in a text file

Hi I need to add a comment line at the begining of a text file. The scenario is given below. 1. The number of servers that needs to be updated is around 80 2. The location of the text file in all the servers are the same including the file name. 3. The comment has to be added at the very... (2 Replies)
Discussion started by: orakhan
2 Replies

10. UNIX for Dummies Questions & Answers

# comment

Hi guys and Gals AIX 5.1 If I had a # above several scripts would it comment out everything below it? Like this # 01 2 ** ..................................... 02 31 ......................................... # this is a script 03 12 **.......................... Would... (6 Replies)
Discussion started by: rocker40
6 Replies
Login or Register to Ask a Question