Compare a column??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare a column??
# 1  
Old 03-15-2010
Compare a column??

Guys,,

I have a csv file. it has few columns... may be 3 or 4. I have the columns to be

1007 | gett | 234
1007 | pitt | 256
1007 | gett | 234
1007 | uitt | 245

Now i need to check all the rows of the first column. All the values present shd be 1007.
even if 1 value changes, i should get an error message.

NOTE : I get the 1007 value from another variable. with that value i should compare the first column in the file. please help me on this..

Thanks in advance
# 2  
Old 03-15-2010
Question what are you trying to find?

Are you trying to list all rows where that first column is NOT 1007?
# 3  
Old 03-15-2010
I just want to check if all the rows are 1007 in that first column... if so, print a "success message", even if one row is not 1007, then i need to print a "Wrong message"
# 4  
Old 03-15-2010
Tools here is a start...

the commnad
Code:
grep -cv "^1007 |" <rgk.csv

will give you the count of lines that do not begin with your desired text
# 5  
Old 03-15-2010
But 1007 is not the value i want.. i just gave it as an example. I get that value from a variable.that part of calculation i ve done. how do i search that obtained value here..

thanks in advance
# 6  
Old 03-15-2010
Code:
value=1007

awk -v var=$value -F"|" '$1 == value{print "Row " NR ": success";next}{print "Row " NR ": wrong"}' file

# 7  
Old 03-15-2010
I am using a file called "test.csv" that is so:

Code:
1007 , gett , 234
1007 , pitt , 256
1007 , gett , 234, 3
1007 , uitt , 245

(note blank lines and the 4 fields on line 3...)

Now the perl script:

Code:
$ perl -nle '$t=/^\s*(1007)\s*,/; die "wrong message\n" if(!$t && /,/);} { print "success!\n\n";' test.csv
success!

If I change any of the "1007" to anything else, I get "wrong message"

Is that what you are looking for?

Or you could use:

Code:
perl -nle 'BEGIN {$s=shift;} $t=/^\s*($s)\s*,/; die "wrong message\n" if(!$t && /,/);} { print "success!\n\n";' 1007 test.csv

if you want values different than "1007"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to compare 3rd column value with first column and display

Hello Team, My source data (INput) is like below EPIC1 router EPIC2 Targetdefinition Exp1 Expres rtr1 Router SQL SrcQual Exp1 Expres rtr1 Router EPIC1 Targetdefinition My output like SQL SrcQual Exp1 Expres Exp1 Expres rtr1 Router rtr1 Router EPIC1 Targetdefinition... (5 Replies)
Discussion started by: sekhar.lsb
5 Replies

2. 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

3. 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

4. UNIX for Dummies Questions & Answers

Compare data - Match first column and compare second

Hi guys, looking for some help with a way to compare data in two files but with some conditions. example, File 1 consists of site1,10.1.1.1 site2,20.2.2.2 site3,30.3.3.3 File 2 contains site1,l0.1.1.1 site2,50.1.1.1 site3,30.3.3.3 site4,40.1.1.1 I want to be able to match the... (1 Reply)
Discussion started by: mutley2202
1 Replies

5. Shell Programming and Scripting

Help with compare two column and print out column with smallest number

Input file : 5 20 500 2 20 41 41 0 23 1 Desired output : 5 2 20 0 1 By comparing column 1 and 2 in each line, I hope can print out the column with smallest number. I did try the following code, but it don't look good :( (2 Replies)
Discussion started by: perl_beginner
2 Replies

6. 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

7. Shell Programming and Scripting

Compare 2 files and match column data and align data from 3 column

Hello experts, Please help me in achieving this in an easier way possible. I have 2 csv files with following data: File1 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:36:09,JOB_5340 08/23/2012 12:36:14,JOB_5340 08/23/2012 12:36:22,JOB_5350 08/23/2012... (5 Replies)
Discussion started by: asnandhakumar
5 Replies

8. 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

9. 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

10. Shell Programming and Scripting

Compare column value against fixed value

Hi all, i have a value something like 1007. I have a excel, where its first column has many values like 1007 1007 1009 1007 1010 etc I need to check if value 1 (ie) 1007 = 1007 if so print yes or i should print no. I should check this through out the column. please help on this. (2 Replies)
Discussion started by: raghulshekar
2 Replies
Login or Register to Ask a Question