Compare columns in a single file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare columns in a single file
# 1  
Old 03-18-2015
Compare columns in a single file

i have the following files (all separated by tabs):

file 1.txt
Code:
1	yes
2	no
3	yes
4	yes

file 2.txt
Code:
a	no
b	no
c	yes
d	no

i combine the above files in file 3 which looks like

file 3.txt
Code:
1	yes	a	no
2	no	b	no
3	yes	c	yes
4	yes	d	no

now, i need to compare the values between column 2 and column 4 and produce file 4 which should look like below

Code:
1	yes	a	no	no match
2	no	b	no	match
3	yes	c	yes	match
4	yes	d	no	no match

any help will be really appreciate. tia

Moderator's Comments:
Mod Comment Use code tags, thanks.
# 2  
Old 03-18-2015
Is that homework?

Code:
$ awk '$2 == $4 {print $0 "\tmatch"; next} {print $0 "\tno match"}' infile
1       yes     a       no      no match
2       no      b       no      match
3       yes     c       yes     match
4       yes     d       no      no match

This User Gave Thanks to zaxxon For This Post:
# 3  
Old 03-18-2015
Or
Code:
awk '{print $0, "\t"($2==$4?"":"no ") "match"}' file3

This User Gave Thanks to RudiC For This Post:
# 4  
Old 03-18-2015
thanks zaxxon & rudic, both works.
nope, sure not a homework for me at least. i am new to awk and i feel i really need to sit down and learn the basics. thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare two date columns in same file

Hi All, Need to compare two date columns from the filname FinalDate.txt. My data's are like below D_OT_START D_EXP_STR Amount 1/3/2012 1/3/2012 5000 6/21/2011 6/25/2011 6000 2/28/2011 2/28/2011 7000 7/16/2010 8/16/2010 8000 7/14/2010 10/26/2010 9000 ... (3 Replies)
Discussion started by: suresh_target
3 Replies

2. Shell Programming and Scripting

Compare Multiple Columns in one file

Hello guys, I am quite new to Shell Scripting and I need help for this I have a CSV file like this: Requisition,Order,RequisitionLineNumber,OrderLineNumber REQ1,Order1,1,1 REQ1,Order1,1,3 REQ2,Order2,1,5 Basically what I want to do is compare the first 3 fields If all 3 fields are the same... (5 Replies)
Discussion started by: jeffreybsu
5 Replies

3. Shell Programming and Scripting

Combining columns from multiple files into one single output file

Hi, I have 3 files with one column value as shown File: a.txt ------------ Data_a1 Data_a2 File2: b.txt ------------ Data_b1 Data_b2 Data_b3 Data_b4 File3: c.txt ------------ Data_c1 Data_c2 Data_c3 Data_c4 Data_c5 (6 Replies)
Discussion started by: vfrg
6 Replies

4. Shell Programming and Scripting

Shell scripting - need to arrange the columns from multiple file into a single file

Hi friends please help me on below, i have 5 files like below file1 is x 10 y 20 z 15 file2 is x 100 z 245 file3 is y 78 z 23 file4 is x 100 (3 Replies)
Discussion started by: siva kumar
3 Replies

5. Shell Programming and Scripting

Fortmating text file - single col to X columns

Hi, I have a large file with 100,000+ lines. Each line have a single value. How do I effiecntly format the text to be X number of commas spaced fields in width. i.e how do i turn this input: 26 53 79 80 81 82 111 131 135 139 192 193 198 199 203 209 (2 Replies)
Discussion started by: carlr
2 Replies

6. Shell Programming and Scripting

script to compare two columns in a file

Dear everyone, I need any sort of shell script or perl script would do the following. I have a txt file as follows: ;Stretnumber Resident Resdient (not in file) 16 John Mary 16 Mary Parker 16 Nancy Smith 16 Mary John 18 Trey ... (5 Replies)
Discussion started by: sasharma
5 Replies

7. UNIX for Dummies Questions & Answers

To compare first two columns in an excel file

Hi All, i have a excel sheet with two columns as below. column1 column2 100 100 200 300 300 400 400 400 500 600 i need to compare the values these two columns and the output should be printed in the third column...if these values are equal the output should be green and if these... (2 Replies)
Discussion started by: arunmanas
2 Replies

8. Shell Programming and Scripting

Filtering issues with multiple columns in a single file

Hi, I am new to unix and would greatly appreciate some help. I have a file containing multiple colums containing different sets of data e.g. File 1: John Ireland 27_December_69 Mary England 13_March_55 Mike France 02_June_80 I am currently using the awk... (10 Replies)
Discussion started by: crunchie
10 Replies

9. Shell Programming and Scripting

merging similar columns in a single line file

Hi Guys. I have tried the commands sort and join. But I couldn't able to to find the command for joining in a single line based on keys.My example inputs and outputs are like the following. Help would be appreciated.:D Input file a1tabXXXXXXX a2tabXXXXXXX a6tabYYYYYYYYY a71tabXXXXXXX... (7 Replies)
Discussion started by: repinementer
7 Replies

10. Shell Programming and Scripting

compare file columns

I need help in file comparision. I have two files in below format: FILE_A: ------- COL1 COL2 COL3 COL4 COL5 FILE_B: ------- COL1A COL1B COL1C COL1D COL1E i want to compare for a for each row in FILE_A and FILE_B COL1 of FILE_A with COL1B of FILE_B COL3 of FILE_A with COL1E of... (1 Reply)
Discussion started by: learnoutmore99
1 Replies
Login or Register to Ask a Question