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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting I need to extract last column of a file and compare the values
# 1  
Old 08-04-2007
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 column is a date field. The date should be unique through out the file, if this column has got more than one value, the script should be exited.

Please help me out.

Thank you
# 2  
Old 08-04-2007
Code:
awk -F", " '{ arr[NF]++; for ( i in arr ) { if ( arr[i] > 1 ) { exit;} } }END{ for ( i in arr ) { print i } }' filename

# 3  
Old 08-04-2007
returning always 1

Thankx for the reply,

I tested this with duplicate values and also unique muliple values in the column of the file and its always returning 1

data in the file

,,02132007
,,03132007

script is returning 1

,,02132007
,,02132007

here also its retruning 1

if all the column values return unique value out of all the records then I need that unique value in my logic. if it returns more than one value then it should exit with some exit code.

can you please modify the script, would be real helpful. I am in urgent need of it.

Thanks
# 4  
Old 08-04-2007
with duplicate values

Code:
cat file
sam, john, 2324, 07142007
tom, thomson, 2343, 07142007
john, scott, 2478, 07142007

Code:
awk -F", " '{ arr[$NF]++; for ( i in arr ) { if ( arr[i] > 1 ) { dup=1; exit} } }END{ if ( dup != 1 ) { for ( i in arr ) { print i } } }' file

<< no output >> as there are duplicates

without duplicate values
Code:
cat file
sam, john, 2324, 17142007
tom, thomson, 2343, 27142007
john, scott, 2478, 07142007

the above command returns
Code:
07142007
27142007
17142007

# 5  
Old 08-04-2007
another way:

Code:
awk -F", " 'arr[$NF] != 0 {exit} { arr[$NF]++ ; cnt++ } END { if ( cnt == NR ){ for ( i in arr ) { print i }}}' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Compare two files column values using awk

Judi # cat File1 judi /export/home 76 judi /usr 83 judi # judi # cat File2 judi /export/home 79 judi /usr 82 judi # if COLUMN3 of File2 is greater that COLUMN3 of File1, then print File2's lines juid /export/home 79 Code tags please (2 Replies)
Discussion started by: judi
2 Replies

3. Shell Programming and Scripting

Extract Oracle home from Oratab and compare values

Friends, I'm trying to do below in ksh script, while requesting user to provide src_db and dest_db values 1. Extract src_db and dest_db Oracle home from oratab file 2. Don't find db if it starts with comment (#) 3. Compare both values 4. Message success or exit with error if they don't... (0 Replies)
Discussion started by: homer4all
0 Replies

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

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

6. UNIX for Dummies Questions & Answers

Compare values of fields from same column with awk

Hi all ! If there is only one single value in a column (e.g. column 1 below), then return this value in the same output column. If there are several values in the same column (e.g. column 2 below), then return the different values separated by "," in the output. pipe-separated input: ... (11 Replies)
Discussion started by: lucasvs
11 Replies

7. Shell Programming and Scripting

Take values from a column and put it in a variable and compare

Hi, I have a table in unix from which i want to read the contents line by line, then filter out the values from 6th column one by one and compare it a fixed value. How to do this? (7 Replies)
Discussion started by: arijitsaha
7 Replies

8. Shell Programming and Scripting

How to compare the values of a column in awk in a same file and consecutive lines..

I would like to compare the values of 2nd column of consecutive lines of same file in such a way so that if the difference between first value and second value is more than 100 it should print complete line else ignore line. Input File ========== PDB 2500 RTDB 123 RTDB-EAGLE 122 VSCCP 2565... (4 Replies)
Discussion started by: manuswami
4 Replies

9. Shell Programming and Scripting

for each different entry in column 1 extract maximum values from column 2 in unix/awk

Hello, I have 2 columns (1st column has multiple entries but the corresponding values in the column 2 may be the same or different.) however I want to extract unique values for each entry in column 1 by assigning the max value from column 2 SDF4 -0.211654 SDF4 0.978068 ... (1 Reply)
Discussion started by: Diya123
1 Replies

10. Shell Programming and Scripting

extract values from column with Perl

Hi everybody I have some problems with PERL programming. I have a file with two columns, both with numeric values. I have to extract the values > 50 from the 2nd columns and sum them among them. The I have to sum the respective values in the first column on the same line and, at the end, I... (6 Replies)
Discussion started by: m_elena
6 Replies
Login or Register to Ask a Question