Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-05-2017, 01:39 PM
|
#1
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240
Rep:
|
subtracting first column with awk
I'm currently reading this: https://docstore.mik.ua/orelly/unix/sedawk/ch07_07.htm
I'm running this script:
Code:
# checkbook.awk
BEGIN { FS = "\t" }
#1 Expect the first record to have the starting balance.
NR == 1 { print "Beginning Balance: \t" $1
balance = $1
next # get next record and start over
}
#2 Apply to each check record, subtracting amount from balance.
{ print $1, $2, $3
print balance -= $3
}
on this file:
Code:
1000
125 Market 125.45
126 Hardware Store 34.95
127 Video Store 7.45
128 Book Store 14.32
129 Gasoline 16.10
The result is not the same as on the site.
It keeps printing the same '1000' instead of being subtracted:
Code:
Beginning Balance: 1000
125 Market 125.45
1000
126 Hardware Store 34.95
1000
127 Video Store 7.45
1000
128 Book Store 14.32
1000
129 Gasoline 16.10
1000
I don't understand what is going on exactly.
|
|
|
05-05-2017, 01:55 PM
|
#2
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,598
|
It's probably your data or your Field Separator (FS). There appears not to be a tab there between the second and third columns. It looks like a bunch of spaces.
Try this to get the last column, using a default FS instead of a tab:
balance -= $NF
Or else try to figure out a pattern for the FS that will work with the separators you actually have in the file.
Edit: you'll probably want to set the Output Field Separator too:
OFS="\t";
Last edited by Turbocapitalist; 05-05-2017 at 02:01 PM.
|
|
2 members found this post helpful.
|
05-05-2017, 02:32 PM
|
#3
|
LQ Guru
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
|
Quote:
Originally Posted by Turbocapitalist
It's probably your data or your Field Separator (FS). There appears not to be a tab there between the second and third columns. It looks like a bunch of spaces.
Try this to get the last column, using a default FS instead of a tab:
balance -= $NF
Or else try to figure out a pattern for the FS that will work with the separators you actually have in the file.
Edit: you'll probably want to set the Output Field Separator too:
OFS="\t";
|
Good call. OP, if you change print $1, $2, $3 to print out each of these variables on a separate line, with a * on either end of each variable's value for clarity, you will notice what's happening here.
|
|
|
05-05-2017, 02:46 PM
|
#4
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240
Original Poster
Rep:
|
Indeed, that was the problem. The third field in the 3rd line, is 'Store', and not the last field. They aren't tabs, but spaces. iterm2 converts tabs to spaces automatically - it asked me once if it should behave so, and I said yes and that was it
|
|
|
All times are GMT -5. The time now is 10:38 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|