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 03-12-2013, 08: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, 09:07 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
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, 09: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 11:06 AM.
 
Old 03-12-2013, 10:12 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
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, 11: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, 12:12 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
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, 12:35 PM   #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, 09:36 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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


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


All times are GMT -5. The time now is 08:31 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration