Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


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 !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-12-2012
Registered User
 
Join Date: Jun 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
How to Detect Specific Pattern and Print the Specific String after It?

I'm still beginner and maybe someone can help me.
I have this input:

Code:
the great warrior a, b, c

and what i want to know is, with awk, how can i detect the string with 'warrior' string on it and print the a, b, and c seperately, become like this :

Code:
Warrior Type
a
b
c

Im still very noob and what i'am already tried is:

Code:
if($0 ~ /warrior/)
print "Warrior Type" $4 $5 $6

but, what if the input string become longer, like ?

Code:
the great alexander warrior of turkey a,b,c

Anybody can give me some help?
Sponsored Links
    #2  
Old 06-12-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,350
Thanks: 144
Thanked 1,756 Times in 1,593 Posts
First you need to define what separates the fields. Gvien your sample it appears to be either spaces or a comma or a combination of both, the field separator would need to be [ \t,]* and to print the last there fields you can use $(NF-2) , $(NF-1) and $NF . To print the result on separate lines, also print newlines, or use separate print statements..[/CODE]
Sponsored Links
    #3  
Old 06-12-2012
Registered User
 
Join Date: Jun 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you!
Can i ask more?
if my input become this:

Code:
warrior a, b, c

And lastly, because there are three warrior a,b, and c, so what should i do to produce output :

Code:
warrior= 3

so it can detect how many warrior there are.
Thanks for your help.
    #4  
Old 06-13-2012
Registered User
 
Join Date: May 2012
Posts: 58
Thanks: 5
Thanked 9 Times in 9 Posts
Response to your last question


Code:
awk -F'[ \t,]*' '{print $1"="NF-1}'

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
Print Specific lines when found specific character attila Shell Programming and Scripting 4 01-26-2012 04:33 AM
Print specific pattern line in c++ cpp_beginner Programming 10 07-07-2011 11:26 AM
search a word and print specific string using awk dragon.1431 Shell Programming and Scripting 7 09-03-2010 07:21 AM
Print out specific pattern column data patrick87 Shell Programming and Scripting 3 12-29-2009 01:05 AM
Print rows, having pattern in specific column... admax Shell Programming and Scripting 25 10-06-2009 06:14 AM



All times are GMT -4. The time now is 04:34 AM.