Visit Our UNIX and Linux User Community


Compare value of a column in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare value of a column in a file
# 1  
Old 09-09-2013
Compare value of a column in a file

i have a file which has few columns.From those columns i need to check the value of column 9 if it is 1 or not.If it is 1 then only i need to print column 4,5,7,9.

Code:
 
Sample input.
 
Column 1,Column 2,Column 3,Column 4,Column 5,Column 5,Column 6,Column 7,column 8,Column 9,Column 10.
 
Heading 1
value 1h,Value 2h,,value 4h,value 5h,Value 6h,Value 7h,Value 8h,1,value 10h
 
Heading 2
value 1b,Value 2b,,value 4b,value 5b,Value 6b,Value 7b,Value 8b,0,value 10b
 
Heading 3
value 1c,Value 2c,,value 4c,value 5c,Value 6c,Value 7c,Value 8c,1,value 10c
 
Output
 
Column 4,Column 5,Column 7,Column 9
value 4h,value 5h,Value 7h,1
value 4c,value 5c,Value 7c,1

In the above input heading 1,heading 2 and heading 3 are various headings in the file.

Also first column header start from line 3 in the file.
# 2  
Old 09-09-2013
Code:
$ awk -F, '$9==1{print $4,$5,$7,$9}' OFS=, input
value 4h,value 5h,Value 7h,1
value 4c,value 5c,Value 7c,1

# 3  
Old 09-09-2013
hi..Thanks for the reply.
but input value can be anything..There are more than 1000 records in the file in a similar pattern.There should be some loop etc.
I dont want to hardcode the input values.
# 4  
Old 09-09-2013
What input value? ... Your problem description is no longer clear to me at all.
# 5  
Old 09-09-2013
The input file has 1000 records of similar pattern as the sample input file that I have given.

However in the script you are hard coding the input values as below
print $4,$5,$7,$9}' OFS=, input
value 4h,value 5h,Value 7h,1
value 4c,value 5c,Value 7c,1

I think there should be some loop which take the values from input .csv file and then redirect the output based on search critaria(where column 9 has value 1) to some output file.
# 6  
Old 09-09-2013
Quote:
Originally Posted by Vivekit82
The input file has 1000 records of similar pattern as the sample input file that I have given.

However in the script you are hard coding the input values as below
print $4,$5,$7,$9}' OFS=, input
value 4h,value 5h,Value 7h,1
value 4c,value 5c,Value 7c,1

I think there should be some loop which take the values from input .csv file and then redirect the output based on search critaria(where column 9 has value 1) to some output file.
No! The script neutronscott gave you was:
Code:
awk -F, '$9==1{print $4,$5,$7,$9}' OFS=, input

which specifies that the input field separator is comma (-F,), the output field separator is comma (OFS=,), and for every line in your input file (you didn't say what the name was, but this code assumes that the input file is named input) if the ninth field is 1 ($9==1) it will print the 4th, 5th, 7th, and 9th fields (print $4,$5,$7,$9). Then he showed that with the input you supplied, it produces the output:
Code:
value 4h,value 5h,Value 7h,1
value 4c,value 5c,Value 7c,1

which is exactly what you asked for. If you have a different name for your input file change input in the script to the name of your input file. If you want to save the output in a file, add
Code:
 > your_desired_output_filename

to the end of the awk command.

This command wil work just fine with zero lines of input or as many input lines as are present in any csv file you give it (as long as you have space to store the output in the file to which you redirect output and input lines are no longer than LINE_MAX bytes for whatever value LINE_MAX is on your system).

Previous Thread | Next Thread
Test Your Knowledge in Computers #39
Difficulty: Easy
IC chips for computer systems are usually made of silicone.
True or False?

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare 1st column from 2 file and if match print line from 1st file and append column 7 from 2nd

hi I have 2 file with more than 10 columns for both 1st file apple,0,0,0...... orange,1,2,3..... mango,2,4,5..... 2nd file apple,2,3,4,5,6,7... orange,2,3,4,5,6,8... watermerlon,2,3,4,5,6,abc... mango,5,6,7,4,6,def.... (1 Reply)
Discussion started by: tententen
1 Replies

2. UNIX for Beginners Questions & Answers

Compare Values between column in the same file

Input File:- COLUMN1 COLUMN2 COLUMN3 COLUMN4 COLUMN5 COLUMN6 SMS Email AO Mail Post N Cell VEGE Potato E W 396 12 0 384 0 0 0 0 0 VEGE Onion S W 17 0 17 0 0 0 0 0 0 FRUIT APPLE N W 549 61 0 0 0 0 0 488 0 FRUIT APPLE SE W 291 14 239 38 0 10 0 0 0 FRUIT APPLE EAMS W 397 32 309 56 309 309 0... (27 Replies)
Discussion started by: Nina2910
27 Replies

3. Shell Programming and Scripting

How to compare the values of a column in a same file using awk?

Dear Unix experts, I have got a file where I would like to compare the values of second column if first column is same in such a way that the difference between the values is >50. If not, I would like to discard both values. For example, my input file looks like - comp275_c0_seq2 73... (7 Replies)
Discussion started by: utritala
7 Replies

4. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

5. Shell Programming and Scripting

Compare the second column of a file with the second column of another in awk

Hi, I know that this topic has been discussed in the past and I've tried to follow all the guidelines. Anyhow, I following describe my problem. I have a file (file1 , no. records = 67) containing pairs of IP addresses as follows (with single space as delimiter between the fields): example... (5 Replies)
Discussion started by: amarn
5 Replies

6. UNIX for Dummies Questions & Answers

Compare two column in file and extract complate line

No Chr Pos Qual GT_1 GT_2 1. chr1 478493 595 A/G G/G 2. chr1 879243 700 A/T T/T 3. chr2 889922 1300 C/C C/C 4. chr2 1926372 300 T/A T/A 5. chr3 237474 500 G/C C/C 6. chr3 575757 700 ... (2 Replies)
Discussion started by: mscott
2 Replies

7. Shell Programming and Scripting

awk compare column n replace with in one file

hi Friends need to compare columns in one file where the data looks like below laptop,IBM phone,samsung car,rental user1,laptop user2,laptop user3,phone want to get output as laptop,IBM phone,samsung car,rental user1,IBM user2,IBM user3,samsung need to seach $2 in array of $1 and... (4 Replies)
Discussion started by: arun1401
4 Replies

8. Shell Programming and Scripting

Compare between column in one file

Hi all, can anyone help me. I want to compare values within column in one file using awk. Below are the sample input file(1st file):... (6 Replies)
Discussion started by: lurak
6 Replies

9. Shell Programming and Scripting

I need to extract last column of a file and compare the values

Hi, I am new to unix and I need help in solving below mentioned issue, really appreciate ur help. I have a file sam, john, 2324, 07142007 tom, thomson, 2343, 07142007 john, scott, 2478, 07142007 its a comma delimited file, I need to extract the last column from each line and this... (4 Replies)
Discussion started by: vukkusila
4 Replies

Featured Tech Videos