The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Remove the comment symbol ' from a file. joyan321 Shell Programming and Scripting 3 06-23-2009 03:38 PM
comment lines vero_81 UNIX for Dummies Questions & Answers 5 06-11-2009 04:35 AM
comment text in a file learnbash Shell Programming and Scripting 8 04-11-2009 02:36 PM
how to comment multiple lines in unix balireddy_77 Shell Programming and Scripting 3 02-19-2007 07:16 AM
Need to add a comment line in a text file orakhan Shell Programming and Scripting 2 09-22-2005 08:36 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-04-2009
phamp008 phamp008 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 36
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 (permalink)  
Old 07-04-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,307
Should be something like this:

Code:
awk '/^c/{f=1}f{$0 = "#" $0}{print}' file
  #3 (permalink)  
Old 07-04-2009
phamp008 phamp008 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 36
Thanks that works well

May I ask where can read on reference for

f{$0 = "#" $0} ?
  #4 (permalink)  
Old 07-04-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361

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 (permalink)  
Old 07-05-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,088
Code:
sed '/c/,$s/^/#/' yourfile
  #6 (permalink)  
Old 07-05-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,535
Code:
awk '/^c/,eof{$0="#"$0}1' file
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:56 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0