Comparing Columns and printing the difference from a particular file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing Columns and printing the difference from a particular file
# 1  
Old 01-12-2010
Comparing Columns and printing the difference from a particular file

Gurus,

I have one file which is having multiple columns and also this file is not always contain the exact columns; sometimes it contains 5 columns or 12 columns. Now, I need to find the difference from that particular file. Here is the sample file:
Code:
param1 | 10 | 20 | 30 |
param2 | 10 | 10 | 10 |
param3 | AB | AB | BC|
param4 | AB | BC | AB |
param5 | AA | AA | AA|

Now, the output print the difference only:
Code:
param1 | 10 | 20 | 30 |
param3 | AB | AB | BC |
param4 | AB | BC | AB |

I tried doing this in awk; but because the number of file columns are not same everytime. so, please help me on this either with awk, perl or any unix solution.

Thanks

Last edited by Scott; 01-12-2010 at 04:36 PM.. Reason: Please use code tags
# 2  
Old 01-12-2010
$ cat data
Code:
param1 | 10 | 20 | 30 |
param2 | 10 | 10 | 10 |
param3 | AB | AB | BC|
param4 | AB | BC | AB |
param5 | AA | AA | AA|

$ awk -F' *\\| *' '{NF--; for(i=2;i<NF;i++) if ($i!=$(i+1)) {print; next}}' data
param1 | 10 | 20 | 30 |
param3 | AB | AB | BC|
param4 | AB | BC | AB |


Last edited by Scott; 01-12-2010 at 04:36 PM.. Reason: Please use code tags
# 3  
Old 01-12-2010
Thanks Alister.
# 4  
Old 01-12-2010
You're very welcome, buzzusa.

The following should give the same results as my first solution (after I posted, I decided to try for something more succinct):

Code:
awk -F' *\\| *' '{for(i=3; i<=NF-1 && $2==$i; i++);} i<NF' data

Take care,
alister
# 5  
Old 01-12-2010
Quote:
Originally Posted by alister
You're very welcome, buzzusa.

The following should give the same results as my first solution (after I posted, I decided to try for something more succinct):

Code:
awk -F' *\\| *' '{for(i=3; i<=NF-1 && $2==$i; i++);} i<NF' data

Take care,
alister
Not understand "i<NF", can you explain?
# 6  
Old 01-12-2010
Hello, rdcwayx:

The empty for loop compares the relevant fields ($2 through $(NF-1) ... NF is always empty) and stops when either a difference has been found or we've run out of fields to compare. If we ran out of fields to compare, i equals NF and i<NF will be false. In this case, nothing happens, as desired. If instead the for loop terminates due to a difference between $2 and some relevant field, $i, then i<NF will be true and the default action of print $0 will be executed.

Hope that clears it up.

Take care,
alister
# 7  
Old 01-13-2010
Quote:
awk -F' *\\| *' ' $0 !~ "\("FS$2"\)\{"NF-2"\}" ' file
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing two columns in two files and printing a third based on a match

Hello all, First post here. I did not notice a previous post to help me down the right path. I am looking to compare a column in a CSV file against another file (which is not a column match one for one) but more or less when a match is made, I would like to append a third column that contains a... (17 Replies)
Discussion started by: dis0wned
17 Replies

2. Shell Programming and Scripting

Awk: Comparing arguments with in line values of file and printing the result

I need to develop a script where I will take two date arguments as parameter date1 and date2 which will in format YYYYMM. Below is the input file say sample.txt. sample.txt will have certain blocks starting with P1. Each block will have a value 118,1:TIMESTAMP. I need to compare the... (7 Replies)
Discussion started by: garvit184
7 Replies

3. Shell Programming and Scripting

Comparing two files with numbers and taking difference in third file

Hi All, I have two files in the following format, with numbers being defined under columns(described by a set of headers) and rows(again defined by a set of identifiers) 2013 2013 Make200 Make201 Merc BMW Jpur Del ... (9 Replies)
Discussion started by: dev.devil.1983
9 Replies

4. Shell Programming and Scripting

Comparing Columns and writing a new file

I have a table with one column File1.txt 1 2 3 4 5 6 7 8 9 10 Another table with two columns; This has got a subset of entries from File 1 but not unique because they have differing values in col 2. File2.txt 1 a 2 d 2 f 6 r 6 e (3 Replies)
Discussion started by: cs_novice
3 Replies

5. Shell Programming and Scripting

Comparing text in 2 files and output difference in another file.

I have 2 files of almost same text apart from 2,3 ending lines. Now I want to get that difference in another file. e.g file1.txt is Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_livecd-lv_root 18G 2.4G 15G 14% / tmpfs 504M ... (12 Replies)
Discussion started by: kashif.live
12 Replies

6. Shell Programming and Scripting

Comparing columns in a file

I have two files. One a small one and another one is big. The smaller one look like this: Filename: 1.tmp 3453 0 326543 1 2321 0 3212 1 The big file looks like this: Filename 1.res 0.3232 2321 9.2922 123 0.983 3212 8.373 326543 0.9 3453 1.098 3432 I want to extract those lines... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

7. UNIX for Dummies Questions & Answers

Comparing the 2nd column in two different files and printing corresponding 9th columns in new file

Dear Gurus, I am very new to UNIX. I appreciate your help to manage my files. I have 16 files with equal number of columns in it. Each file has 9 columns separated by space. I need to compare the values in the second column of first file and obtain the corresponding value in the 9th column... (12 Replies)
Discussion started by: Unilearn
12 Replies

8. UNIX Desktop Questions & Answers

COMPARING COLUMNS IN A TEXT FILE

Hi, Good day. I currently have this data called database.txt and I would like to check if there are no similar values (all unique) on an entire row considering the whole column data is unique. the data is as follows cL1 cL2 cL3 cL4 a12 c13 b13 c15 b11 a15 c19 b11 c15 c17 b13 f14 with... (1 Reply)
Discussion started by: whitecross
1 Replies

9. Shell Programming and Scripting

Columns comparision of two large size files and printing the difference

Hi Experts, My requirement is to compare the second field/column in two files, if the second column is same in both the files then compare the first field. If the first is not matching then print the first and second fields of both the files. first file (a .txt) < 1210018971FF0000,... (6 Replies)
Discussion started by: krao
6 Replies

10. Shell Programming and Scripting

Help with comparing columns from a csv file

Hi there, I have an csv file. I want to compare the 16th and 18th columns. They contain alpha numeric characters. Some are same and some are different. We have to pick the ones which are different. But with certain rules. 16th col. 18th col. ---------- ... (1 Reply)
Discussion started by: sickboy
1 Replies
Login or Register to Ask a Question