LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 05-05-2017, 01:39 PM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Rep: Reputation: 103Reputation: 103
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.
 
Old 05-05-2017, 01:55 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,598
Blog Entries: 4

Rep: Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894
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.
Old 05-05-2017, 02:32 PM   #3
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by Turbocapitalist View Post
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.
 
Old 05-05-2017, 02:46 PM   #4
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
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
 
  


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
[SOLVED] awk IP whatever where it is at the column of syslog Aeolustw Programming 15 09-08-2016 10:01 AM
[SOLVED] AWK fill column from previuos line column akeka Programming 4 01-30-2013 08:16 PM
[SOLVED] Help with subtracting column 2 and column 1 and print the result codergeek Programming 4 12-23-2012 02:31 PM
[SOLVED] Subtracting the value of a specific cell to the whole column udiubu Programming 13 11-11-2011 11:50 AM
awk multiple column into single column ilukacevic Programming 49 07-19-2010 08:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:38 PM.

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