The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to extract only first column from the file selamba_warrior Shell Programming and Scripting 11 05-21-2008 03:52 AM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 09:57 AM
Aggregate values in a file & compare with sql output shiroh_1982 UNIX for Dummies Questions & Answers 1 09-02-2007 08:40 AM
Extract values from log file wdympcf Shell Programming and Scripting 5 08-10-2007 04:52 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-04-2007
vukkusila vukkusila is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 6
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 (permalink)  
Old 08-04-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,960

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

  #3 (permalink)  
Old 08-04-2007
vukkusila vukkusila is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 6
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 (permalink)  
Old 08-04-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,960
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 (permalink)  
Old 08-04-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,246
another way:


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

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:08 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0