The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 do i pattern match a field with awk? someone123 Shell Programming and Scripting 4 06-03-2008 08:08 AM
match field between 2 files phamp008 Shell Programming and Scripting 8 01-23-2008 07:46 AM
regexp to print after a field seperator ramky79 Shell Programming and Scripting 7 05-09-2007 06:31 PM
egrep field print knc9233 UNIX for Dummies Questions & Answers 2 02-14-2007 02:07 PM
match a pattern, print it and the next line nymus7 Shell Programming and Scripting 4 07-29-2005 12:59 AM

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

Join Date: Sep 2006
Location: Sg
Posts: 350
Print last occurrence if first field match

Hi All,

I have an input below. If the term in the 1st column is equal, print the last row which 1st column is equal.In the below example, it's " 0001 k= 27 " and " 0004 k= 6 " (depicted in bold). Those terms in 1st column which are not repetitive are to be printed as well. Can any body help me by using awk or perl ? Thanks


Input:

0001 k= 1
0001 k= 26
0001 k= 27
0002 k= 1
0003 k= 1
0004 k= 1
0004 k= 4
0004 k= 5
0004 k= 6

Output:

0001 k= 27
0002 k= 1
0003 k= 1
0004 k= 6
  #2 (permalink)  
Old 11-15-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
assuming your input is sorted based on the first column...

nawk -f ray.awk myInputFile

ray.awk:
Code:
# if the value in the first column ($1) does not equal the value stored in "prevIDX"
# and we are not at the FIRST record (FNR != 1) [FNR = RecordNumber in the current File]- print the value of "prev"
$1 != prevIDX && FNR != 1 { print prev}

# assign the value of the FIRST field ($1) to a variable "prevIDX"
# assign the entire record/line to the variable "prev"
{ prevIDX=$1; prev=$0 }

# after ALL the records/lines were processed - print the value of the variable "prev" 
# the last/dangling record/line
END { print prev }

Last edited by vgersh99; 11-15-2007 at 11:17 AM..
  #3 (permalink)  
Old 11-15-2007
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
Quote:
Originally Posted by vgersh99 View Post
assuming your input is sorted based on the first column...

nawk -f ray.awk myInputFile

ray.awk:
Code:
$1 != prevIDX && FNR != 1 { print prev}
{ prevIDX=$1; prev=$0 }
END { print prev }
Hi vgersh99,

That's cool!! It works!! You a are brillant!!
But i ' m new to the " prev " and " FNR " term.
Can you explain your code so that i can understand better ?
  #4 (permalink)  
Old 11-15-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
see the comments in the original post.
  #5 (permalink)  
Old 11-16-2007
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
Hi vgersh99,

Can i use " NR " instead of " FNR " for this case this there's only one input file ?
  #6 (permalink)  
Old 11-16-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by Raynon View Post
Hi vgersh99,

Can i use " NR " instead of " FNR " for this case this there's only one input file ?
don't see why not.....
  #7 (permalink)  
Old 03-13-2008
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
Hi vgersh99,

If i need to capture the 1st occurrence instead that is if column1 of row1 equals to column1 of row2 then the whole line of column1 row1 is being printed out , how can i modify your code below to fulfill this requirement?

Code:
$1 != prevIDX && FNR != 1 { print prev}
{ prevIDX=$1; prev=$0 }
END { print prev }
Input:

0001 k= 1
0001 k= 26
0001 k= 27
0002 k= 1
0003 k= 1
0004 k= 1
0004 k= 4
0004 k= 5
0004 k= 6

Output:

0001 k= 1
0002 k= 1
0003 k= 1
0004 k= 1

Last edited by Raynon; 03-13-2008 at 07:07 AM..
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 04:35 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