Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-24-2009, 04:48 PM   #1
LQ Newbie
Registered: Apr 2009
Posts: 4

Rep: Reputation: 0
Change column value with AWK

Hi everyone,

what I have is a script that calculates an amount

amt=`echo $price*$qty*1.13 | bc`

after this is calculated, I need to somehow use AWK to add this value to the second column of my file. The file looks like this:


I have a variable "$name" that has been entered by the user, say "dan" is entered I need to search through the file and add 13760 + $amt. so far I have this but its not working... and I think its too complicated for this task

awk -F":" '$1~/'$name'/ {total=$2+$amt; printf "%s:%i\n",$1,$total}' $a && awk -F":" ' ! $1~/'$name'/ { print $0}' $a > file

so what im trying to do is make an output and then just redirect it to the file...

but there has to be an easier way of doing this? like by just adding $2 with $amt or something? or use sed?

any help is greatly appreciated
Old 04-24-2009, 05:45 PM   #2
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,191

Rep: Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039
I assume that you have a shell script which calculates the value for $amt somehow.

Then in this shell script you call:
awk -v amt=$amt -v name=dan -f add.awk names.txt
Now you can use the variables name and amt in the awk script. Although you can put it on one line, it is easier to create an .awk file for it.

if (name == $1)
        print "was: " $1 " " $2 " becomes: "$1 " " $2+amt
        print "was: " $1 " " $2 " remains: "$1 " " $2
awk -v amt=1000 -v name=dan -f add.awk names.txt
This yields:
was: dan 13760 becomes: dan 14760
was: ricky 1235 remains: ricky 1235
was: mitch 123 remains: mitch 123
This is not exactly the output format you need, but I wrote it like this for clarity. You can easy adapt it to your preferences.

To pipe everyting into a file, you can append "> outputfile" to the awk command.

Remember there is NO way that inside and awk script you can recognize a bash variable. All values have to be passed thru the command line using the form -v var1=foo -v var2=bar.. etc.

Old 04-25-2009, 04:05 AM   #3
LQ Newbie
Registered: Apr 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks alot for the help! I finished my script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
sed/awk group on first column Eddie Adams Linux - General 4 04-09-2009 11:23 AM
awk comparing a column value with a stored variables value bugg_deccan Programming 4 12-05-2008 08:08 AM
Multiplying data column in awk - can rounding be suppressed? johnpaulodonnell Linux - Newbie 2 02-28-2007 08:56 AM
sed / awk command to print line number as column? johnpaulodonnell Linux - Newbie 2 01-22-2007 08:07 AM
awk column printing schneidz Programming 7 09-29-2005 07:14 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:24 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration