943,807 Members | Top Members by Rank

Ad:
Mar 12th, 2009
0

Adding a new column in a text file

Expand Post »
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 ??
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
snahata is offline Offline
1 posts
since Mar 2009
Mar 15th, 2009
0

Re: Adding a new column in a text file

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. $
Reputation Points: 22
Solved Threads: 11
Junior Poster
fpmurphy is offline Offline
144 posts
since Oct 2008
Mar 17th, 2009
0

Re: Adding a new column in a text file

Shell Scripting Syntax (Toggle Plain Text)
  1. awk '{printf "%s %s",$0, ($3 > 40)? "yes" :"no"}' file
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: using grep, sed, awk to pull links out of an XML file
Next Thread in Shell Scripting Forum Timeline: More Graphical screens





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC