LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 04-24-2009, 03:48 PM   #1
RyudoBlaze
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:

dan:13760
ricky:1235
mitch:123

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, 04:45 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

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

Then in this shell script you call:
Code:
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.

names.txt:
Code:
dan:13760
ricky:1235
mitch:123
add.awk
Code:
BEGIN{
FS=":"
}
{
if (name == $1)
        print "was: " $1 " " $2 " becomes: "$1 " " $2+amt
else
        print "was: " $1 " " $2 " remains: "$1 " " $2
}
Calling:
Code:
awk -v amt=1000 -v name=dan -f add.awk names.txt
This yields:
Code:
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.

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

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


Reply


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 10:23 AM
awk comparing a column value with a stored variables value bugg_deccan Programming 4 12-05-2008 07:08 AM
Multiplying data column in awk - can rounding be suppressed? johnpaulodonnell Linux - Newbie 2 02-28-2007 07:56 AM
sed / awk command to print line number as column? johnpaulodonnell Linux - Newbie 2 01-22-2007 07:07 AM
awk column printing schneidz Programming 7 09-29-2005 06:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:58 AM.

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