LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-04-2012, 06:49 PM   #1
oliviaxinw
LQ Newbie
 
Registered: Jul 2012
Posts: 3

Rep: Reputation: Disabled
Reading difference


Hi,

I have a file that looks like this:
Basically, in column 1, for every pair of two lines, the number before "/" are the same.
I want to be able to pull out all the lines in which the column2 is different for the pair of two lines.
For example, in the lines above, I would want to have a output file that prints the last two lines because 258975 and 258975 are two different numbers for 254879/1 and 254879/2. What code should I use?

Column1 Column2
123456/1 123456
123456/2 123456
235648/1 234567
235648/2 234567
254879/1 258975
254879/2 265897
.... .....

Thank you.
 
Old 10-05-2012, 03:05 PM   #2
kabamaru
Member
 
Registered: Dec 2011
Location: Greece
Distribution: Slackware
Posts: 276

Rep: Reputation: 133Reputation: 133
Code:
awk -F / '{ print $1, $2 }' FILENAME | uniq -f2 -u | awk ' { print $1 "/" $2, $3 }'

Last edited by kabamaru; 10-05-2012 at 03:32 PM.
 
Old 10-05-2012, 03:40 PM   #3
kabamaru
Member
 
Registered: Dec 2011
Location: Greece
Distribution: Slackware
Posts: 276

Rep: Reputation: 133Reputation: 133
And to explain this...

awk -F / '{ print $1, $2 }' FILENAME uses '/' as field separator and prints lines like this:

Code:
123456 1 123456
123456 2 123456
235648 1 234567
235648 2 234567
254879 1 258975
254879 2 265897
Then uniq -f2 -u prints only lines that have no duplicates, ignoring the first 2 fields:

Code:
254879 1 258975
254879 2 265897
And awk ' { print $1 "/" $2, $3 }' restores the '/1' or '/2' part:

Code:
254879/1 258975
254879/2 265897

Last edited by kabamaru; 10-05-2012 at 04:20 PM.
 
Old 10-05-2012, 04:41 PM   #4
kabamaru
Member
 
Registered: Dec 2011
Location: Greece
Distribution: Slackware
Posts: 276

Rep: Reputation: 133Reputation: 133
Hmm, now that I think of it, this won't work as expected if two pairs of lines have the same number in second column...

If the '/1' or '/2' part doesn't need to be displayed, you can use this command:

Code:
sed 's/\/[12]//' FILENAME | uniq -u
 
Old 10-05-2012, 05:03 PM   #5
kabamaru
Member
 
Registered: Dec 2011
Location: Greece
Distribution: Slackware
Posts: 276

Rep: Reputation: 133Reputation: 133
Ok, found one that will do the job:

Code:
sed 's/\([0-9]*\)\(\/[12]\) \([0-9]*\)/\2 \1 \3/' FILENAME \
  | uniq -f1 -u \
  | awk '{ print $2 $1, $3 }'

Last edited by kabamaru; 10-05-2012 at 05:06 PM.
 
Old 10-07-2012, 06:09 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Code:
awk 'NR%2==1 {a=$2;b=$0} ; NR%2==0 && $2!=a {print b"\n"$0}' infile
On odd-numbered lines, the second field is stored in variable a, and the whole line into variable b.

On even numbered lines, the second field is compared to the previous value (a), and if different, the last line (b) and the current line are printed.
 
  


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
Microsoft Visual C++, Reading from File, Reading an XFile not heirarchy Soulkeeper1987 Programming 1 07-01-2012 04:09 PM
My dvd drive is not mounting and reading. No udf based dvd is not reading. anjanatsuse Linux - Newbie 1 10-15-2010 12:54 PM
scanf reading newline into char array while reading 1 char at a time austinium Programming 6 09-26-2010 11:27 PM
LXer: Making a Difference; Selling a Difference LXer Syndicated Linux News 0 09-23-2010 05:00 AM
shell script to find the difference betwwn two file and place the difference to other kittunot4u Linux - General 3 07-19-2010 04:26 AM


All times are GMT -5. The time now is 01:38 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration