LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 03-12-2013, 07:59 AM   #1
secret
LQ Newbie
 
Registered: Mar 2013
Posts: 9

Rep: Reputation: Disabled
awk comparing 2 rows and counting


Hi. I have a file that looks like this

1 1 2 3
2 1 3 2
3 1 2 3
4 2 1 3

and so on. column 1 describes a time and the rest are temperatures that may swap.
Id like to compare in column 2 to 3 line 1 with line 2. If the value is the same count 0 if it differs count 1. Then compare line 2 and 3 and so on till the end. In the end i want to know how many times the value of each line in column 2-4 changed. Id like to use a script in awk. But i got no clue how to define this.
awk '{current = $NF;getline; if($NF == current}print "match";else print "mismatch"}' file
this i found in another thread. It compare the lines and tells if its a match or mismatch. Instead of such an output id like a count at the end of how many mismatches there have been i think.
Thanks for the help
 
Old 03-12-2013, 08:07 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
You'll have to "remember" the values from the last line in each iteration. So, for each line:
1) compare the values stored from the last iteration to the values from the current line.
2) if the numbers are different, increment counter
3) copy the values from the current line to the variables so you can access them in the next iteration.

You will also have to think about how to treat the first line.
 
Old 03-12-2013, 08:33 AM   #3
secret
LQ Newbie
 
Registered: Mar 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
Ok sry for the german. What i said was i think maybe like this?

awk ' {current=$NF ; getline ; if ($NF != current)} print "++; else 0"

Though the 0 could mean that its not adding but writing just zero when there is no change.
The first row cant be compared to anything before so i guess the value should be 0? Ive never programmed anything before so im kinda confused :/

maybe more like this?


awk '{current=$NF ; getline ; if($NF!=current{count++})} print "count"

Last edited by secret; 03-12-2013 at 10:06 AM.
 
Old 03-12-2013, 09:12 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Ok ... so putting that into google translate helped a little

So getline is not needed at all. NR is the current line count so this could be used to know when at line 1 or elsewhere.

When needing to print something when you are finished getting your data you need to investigate the END{} clause.

Here is the link to the manual online which I recommend reading:

http://www.gnu.org/software/gawk/man...ode/index.html

Read over millgates information again and use the page above it should be fairly straight forward.
 
Old 03-12-2013, 10:55 AM   #5
secret
LQ Newbie
 
Registered: Mar 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
as i understand
current=$NF defines that the line read at the moment is stored as a variable called NF so when the next line is read it can compare the new current line with the line before.
then i have to tell it to actually compare by $NF!=current and somehow tell it if that is true count +1. ++ is the same as +1 right?
Then i want to keep track of the total count and get the total number printed. I dont need to know which matched and which didnt i only need a total count.
Also it would be nice if this could be done in one step for each column so i get a count for each column. To be honest i do understand what should be done even before i new what awk was BUT even with the manual it doesnt say anything about counts (not that i saw anything). You have to understand i have never programed anything before. So aside from the getline how should my idea be modified?
should look like this atm:

awk '{current=$NF , if($NF != current {count++})} END{print count}'

but this still doesnt answer how the first line should be treated nor if this works for each column individually
 
Old 03-12-2013, 11:12 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Quote:
current=$NF defines that the line read at the moment is stored as a variable called NF so when the next line is read it can compare the new current line with the line before.
Incorrect. NF is the Number of Fields in a row which is determined by the FS (Field Separator), which as you have not changed it is the default of any contiguous white space.
So in the example, current is being set to the value of whatever is stored in the last field. So in your example data the first line would store the number 3 in current as it is the last field.
Quote:
++ is the same as +1 right?
Correct:
Code:
count++
# is the same as
count = count + 1
So you are kind of on the right track but need to see my information above about NF.

Assuming your syntax was right, which it currently is not, if you issue the following:
Code:
awk 'NR > 1 && $NF != current{count++}{current = $NF}NR == 1{next}END{print count}' file
See if that helps you in the correct direction.
 
Old 03-12-2013, 11:35 AM   #7
secret
LQ Newbie
 
Registered: Mar 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
My Tutor pointed out i should just try things step by step and gave me the same hint as you about NF (>.<)
So i changed it slowly by trying and came to

awk 'BEGIN {count=0;var=0}{if ($2!=var) count++; var=$2} END {print count-1}' input

and it worked yeahi. My head hurts.
So thank you all very much for your patience
 
Old 03-12-2013, 08:36 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
Quote:
My Tutor pointed out i should just try things step by step ..
and that is the secret of successful programming, unless you are already highly experienced, in which case you can often get away with larger changes
 
  


Reply



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
Counting the amount of lines in a file and comparing the result to a variable in BASH doucettej3 Linux - Newbie 5 02-04-2013 11:20 AM
[SOLVED] awk columns to rows not working toney_e Programming 13 09-25-2012 01:52 AM
[SOLVED] merging files with different number of rows using awk linux002 Linux - Newbie 8 05-05-2011 03:26 PM
Value counting in awk scripts sarajevo Programming 9 10-20-2006 08:24 AM
I there a way to tell awk to select rows instead of collums jsandro7 Linux - Newbie 2 06-14-2004 10:58 AM

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

All times are GMT -5. The time now is 09:33 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