using awk to comment out lines to the end of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using awk to comment out lines to the end of file
# 1  
Old 07-04-2009
using awk to comment out lines to the end of file

Hello,

I have a file as follow

a
b
c
c
d
d
e


I would like to write a awk command to insert # from the first occurence of
"c" to the end of the files.
OUTPUT should be like this

a
b
#c
#c
#d
#d
#e

I kept getting error while attempting to use break

awk ' if ( $1 ~ /^c/ ) { beginline = NR ; break } '

Greatly appreciate your comment
# 2  
Old 07-04-2009
Should be something like this:

Code:
awk '/^c/{f=1}f{$0 = "#" $0}{print}' file

# 3  
Old 07-04-2009
Thanks that works well

May I ask where can read on reference for

f{$0 = "#" $0} ?
# 4  
Old 07-04-2009

Code:
awk '/^c/{f=1}     ## If the first character of the line is "c" set f = 1
  f {$0 = "#" $0}  ## if f is not equal to 0, add "#" to beginning of line
  {print}          ## print every line
' file

# 5  
Old 07-05-2009
Code:
sed '/c/,$s/^/#/' yourfile

# 6  
Old 07-05-2009
Code:
awk '/^c/,eof{$0="#"$0}1' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove Comment Lines From Script/Input File

Hello, I have a SAS code that predominantly has comments line and the real code like below and i want to remove ONLY THE COMMENTS from the code in the single line or spanned across multiple lines. /******************************************************************** *** This Is a Comment... (4 Replies)
Discussion started by: arooonatr
4 Replies

2. Shell Programming and Scripting

Script using awk to find and replace a line, how to ignore comment lines

Hello, I have some code that works more or less. This is called by a make file to adjust some hard-coded definitions in the src code. The script generated some values by looking at some of the src files and then writes those values to specific locations in other files. The awk code is used to... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

3. Shell Programming and Scripting

Find between lines start and end: awk with bash variables

I have a file as follows: 0 1056 85540 414329 774485 1208487 1657519 2102753 2561259 3037737 3458144 3993019 4417959 4809964 5261890 5798778 6254146 I want to find all lines between a specified start and end tag. (6 Replies)
Discussion started by: jamie_123
6 Replies

4. Shell Programming and Scripting

Put the lines from file A to end of lines in file B

I really can't figure this one out. I have 2 files, one file is a list of hostnames and the other is a list of their corresponding IPs: fileA: example.com another.org thirdie.net fileB: 1.1.1.1 2.2.2.2 3.3.3.3 I want to create a fileC that looks like: example.com 1.1.1.1... (2 Replies)
Discussion started by: zstar
2 Replies

5. Shell Programming and Scripting

Comment ( -- ) lines from 10 to 25 for every .sql file

Platform : RHEL 5.4 I have several .sql files in a directory. I want to comment lines 10 to 25 for all .sql files. How can I do this ? The symbol for comment in SQL is -- eg: -- select salary from emp where empname = 'URS' ; (3 Replies)
Discussion started by: omega3
3 Replies

6. UNIX for Dummies Questions & Answers

Comment lines in file without vi editor

Legends, Can you please help me in following. I need to comment lines from “/tmp/a.txt” from the line A to line B through the command prompt only. Please use variables not direct values like 2 or 5 It can be done with VI editor but it's not matches with my requirement (: 2,5 s/^/#/g). ... (1 Reply)
Discussion started by: sdosanjh
1 Replies

7. Shell Programming and Scripting

sed/awk script to replace only FIRST comment in the file

My first comment on every file contains the license message. I want to replace with a new license message. I used the below sed script, which replaces all comments. What is the modification or any other method with awk script for the below to edit only the first comment(license message)? #sed -f... (1 Reply)
Discussion started by: vpshastry
1 Replies

8. Shell Programming and Scripting

how to find and comment out lines in one file against another file

I have 2 files: fileA and fileB. content of fileA --------------- admin.teacher is in new york; admin.mason is in new york; admin.driver is in new york city; user.trucker is in hartford; admin.developer is in new york state; content of fileB ---------------- admin.teacher is in... (2 Replies)
Discussion started by: lowprofile
2 Replies

9. Shell Programming and Scripting

deleting the lines at the end of the file.

I have a text file with two coulmn first column is just used in to show the line number, these line number are not there in the real file. I want to delete the line 16(in this file) here, even tough there is no data inside it . this empty line is causing me a problem by throwing me garbage... (12 Replies)
Discussion started by: shashi792
12 Replies

10. Shell Programming and Scripting

Blank Lines - End of file

Hi all I need to strip blank lines from the end of a file. I have searched and found topics on how to strip lines from the entirety of a file - however I need to limit this to only the last 3-4 lines. Any ideas? Thanks (4 Replies)
Discussion started by: saabir
4 Replies
Login or Register to Ask a Question