|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
Response to your last question Code:
awk -F'[ \t,]*' '{print $1"="NF-1}' |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|