How to compare two text files in column wise?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to compare two text files in column wise?
# 1  
Old 03-19-2009
How to compare two text files in column wise?

Hi All,

I have two txt files like this

File1:
no name
----------
12 aaaa
23 bbbb
55 cccc

File2
dname dno
------------
civil 33
mech 55
arch 66

Now i want to compare col1 from File and col2 from File2, if its match i want fetch all columns from two files.

Expected output:
no name dname dno
----------------------
55 cccc mech 55

How can we do that?

help me on this

Thanks
MPS
# 2  
Old 03-19-2009
Use nawk, /usr/xpg4/bin/awk or gawk on Solaris:

Code:
awk 'NR == FNR {
  _[$1] = $0
  next
  }
$2 in _ {
  print _[$2], $0
  }' file1 file2

# 3  
Old 03-19-2009
Hi radoulov,

its giving some "Parse" error when i issue the code in command prompt as well as i tried with awk script lik this.


$ cat com.awk
{
NR == FNR {
_[$1] = $0
next
}
$2 in _ {
print _[$2], $0
}
}


then in Command prompt

$ awk -f com.awk file1 file2

even though its not working.


Thanks
MPS
# 4  
Old 03-19-2009
Did you try with nawk/XPG awk as suggested? What platform you're using?

P.S. The content of com.awk is wrong, you should remove the outer braces.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

2. UNIX for Advanced & Expert Users

Parse output and then compare column wise with awk

Hello, I am trying to write a script to parse the output of data and then alert based on certain conditions This is the output of my script (STRING) Name = Joe (FLOAT64) BMI = 34 (FLOAT64) Weight = 156 (STRING) Name = Sam (FLOAT64) BMI = 32 (FLOAT64) Weight = 180 and so on it repeats... (4 Replies)
Discussion started by: sidnow
4 Replies

3. Shell Programming and Scripting

Search string in multiple files and display column wise

I have 3 files. Each of those files have the same number of records, however certain records have different values. I would like to grep the field in ALL 3 files and display the output with only the differences in column wise and if possible line number File1 Name = Joe Age = 33... (3 Replies)
Discussion started by: sidnow
3 Replies

4. Shell Programming and Scripting

Column wise text adding

Hi I have pasted sample data as below:- in data.txt Please suggest any way out: as the 3rd field is cat data.txt 22:37:34 STARTING abc 22:37:40 FAILURE sadn 00:06:42 STARTING asd 00:06:51 FAILURE ad 02:06:38 STARTING acs 02:06:46 FAILURE cz 04:06:35 STARTING xzc... (1 Reply)
Discussion started by: Gaurav198
1 Replies

5. Red Hat

How to find a garbage entry in a column wise text file in Linux?

Suppose I have a file containing :- 1 Apple $50 2 Orange $30 3 Banana $10 4 Guava $25 5 Pine@apple $12 6 Strawberry $21 7 Grapes $12 In the 5th row, @ character inserted. I want through sort command or by any other way this row should either on top or bottom. By sort command garbage... (1 Reply)
Discussion started by: Dipankar Mitra
1 Replies

6. Shell Programming and Scripting

Compare Two Files(Column By Column) In Perl or shell

Hi, I am writing a comparator script, which comapre two txt files(column by column) below are the precondition of this comparator 1)columns of file are not seperated Ex. file1.txt 8888812341181892 1243548895685687 8945896789897789 1111111111111111 file2.txt 9578956789567897... (2 Replies)
Discussion started by: kumar96877
2 Replies

7. Shell Programming and Scripting

Combine Multiple text or csv files column-wise

Hi All I am trying to combine columns from multiple text files into a single file using paste command but the record length being unequal in the different files the data is running over to the closest empty cell on the left. Please see below. What can i do to resolve this ? File 1 File... (15 Replies)
Discussion started by: venky_ibm
15 Replies

8. Shell Programming and Scripting

Compare files column to column based on keys

Here is my situation. I need to compare two tab separated files (diff is not useful since there could be known difference between files). I have found similar posts , but not fully matching.I was thinking of writing a shell script using cut and grep and while loop but after going thru posts it... (2 Replies)
Discussion started by: blackjack101
2 Replies

9. Shell Programming and Scripting

URGENT: Script/Function needed to read text property files in block wise

Hi, Iam in a need for a script/function in KSH where I want to read a text file (property file) in block by block. Here is the example: Heading Name Descripton Block Block1 Value1 Description Property Name Value Property Name Value Property Name Value Property Name Value Property Name... (7 Replies)
Discussion started by: ysreenivas
7 Replies

10. Shell Programming and Scripting

column compare of files

Hi i want to compare files a.txt 12345,23 34567,76 65456,10 13467,01 b.txt 12346,23 34567,76 23333,90 65456,10 13467,03 i want o/p in 3 files common.txt both have (2 Replies)
Discussion started by: aaysa123
2 Replies
Login or Register to Ask a Question