How to print first occurence | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

How to print first occurence

UNIX for Dummies Questions & Answers


Tags
first occurrence

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-12-2012
BMatter BMatter is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 14 October 2012, 11:23 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
How to print first occurence

Hi there,
how can i print the first pattern occurrence in a .log file?

I want to print the filename of the first 17262?
I tried but all I can do is print all the lines with the number 17262?
I tried using awk and sed but nothing!
I just want filename!

Here´s an example:

Code:
17259 1349258032 773472 PS Some text
17262 1349258032 773735 CE Some text
17267 1349258052 351658 CS tests/filename Some text
17267 1349258052 352041 QS Some text
17259 1349258052 352109 QR Some text
17259 1349258052 352432 PS Some text
17267 1349258052 375328 CE Some text

Can someone help me!

Thanks!

Moderator's Comments:
Please view this code tag video for how to use code tags when posting code and data.
Sponsored Links
    #2  
Old 10-12-2012
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 9 September 2014, 8:08 AM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 Posts
I assume that you meant 17267 instead of 17262 .

Code:
awk '/^17267/{sub(/.*\//,"",$5);print $5;exit(0)}' file


Last edited by elixir_sinari; 10-12-2012 at 10:07 AM..
Sponsored Links
    #3  
Old 10-12-2012
BMatter BMatter is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 14 October 2012, 11:23 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks but with
Code:
awk '/^17267/{sub(/.*\//,"",$5);print $5;exit(0)}' file

i have no answer from terminal.

Can you explain your solution?

Thanks!

Last edited by jim mcnamara; 10-13-2012 at 12:32 AM..
    #4  
Old 10-12-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 15 September 2014, 8:06 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts
Or


Code:
awk -F/ '/^17267/{split($NF,a," ");print a[1];exit(0)}' file

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Number each occurence using sed xerox Shell Programming and Scripting 5 01-17-2012 06:43 AM
print lines between two strings but for the first occurence. solved Sangal_ak04 Shell Programming and Scripting 0 04-26-2010 05:40 PM
Replacing nth occurence raghav288 Shell Programming and Scripting 11 10-29-2009 11:39 AM
Print row if value in column 1 is the first occurence Raynon Shell Programming and Scripting 7 03-18-2008 02:31 AM
First Occurence kkm_job UNIX for Advanced & Expert Users 5 10-30-2006 05:37 PM



All times are GMT -4. The time now is 06:25 PM.