Adding a new column in a text file

Reply

Join Date: Mar 2009
Posts: 1
Reputation: snahata is an unknown quantity at this point 
Solved Threads: 0
snahata snahata is offline Offline
Newbie Poster

Adding a new column in a text file

 
0
  #1
Mar 12th, 2009
Hi,

I would like to add a new column in a text file which contains values based on the value of another column.
Eg:
suppose the text file contains:
1 abc 67
2 def 40
3 uty 57
.......
....
Now i would like to add a new column which contains value 'yes' if the column 3 > 40 else 'no'.
Is it possible using awk ??
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 95
Reputation: fpmurphy is an unknown quantity at this point 
Solved Threads: 5
fpmurphy fpmurphy is offline Offline
Junior Poster in Training

Re: Adding a new column in a text file

 
0
  #2
Mar 15th, 2009
Shell Scripting Syntax (Toggle Plain Text)
  1. $ awk '{ if ($3 > 40) four="yes"; else four="no"; print $0, four }' file
  2. 1 abc 67 yes
  3. 2 def 40 no
  4. 3 uty 57 yes
  5. $
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Adding a new column in a text file

 
0
  #3
Mar 17th, 2009
Shell Scripting Syntax (Toggle Plain Text)
  1. awk '{printf "%s %s",$0, ($3 > 40)? "yes" :"no"}' file
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC