Register
It is currently Sat Oct 25, 2014 11:09 pm

awk search help


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Wed Jul 02, 2014 3:36 pm   

Joined: Thu Jun 26, 2014 7:26 am
Posts: 3
Hi, I need help with searching for certain matching letters on a lines.

Say for example

Code:
Name       VT       Kip     Rand
NINA         A         K       kevdhekdmabr2738
NINA          J         P       pdbkemgwjsabeb483
NINA         K        M       meveahsbkdemjep48328
NINA        A         M       ehkmskdldadvsvpm46383
NINA        KFF      KF      zbdamshasjerrj3738
NINA         J          K       amfjekspdbejd34549


My goal is to be able to find matches with A and M
and K and J as VT and KMP. As you can see the all the the Rand column has a series of letters that match my needed letters.

I have tried
Code:
awk '/nina/ && /K/ && /J/ && /A/ && /M/ {print $0}' sampfile.txt


Top
 Profile  
 PostPosted: Wed Jul 02, 2014 5:01 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
hi,

basically, second AND third fields have to be A or J or K or M, is that right?
then
Code:
awk --assign pat="^[AJKM]\$" '$2 ~ pat && $3 ~ pat' yourFile


Top
 Profile  
 PostPosted: Thu Jul 03, 2014 10:35 am   

Joined: Thu Jun 26, 2014 7:26 am
Posts: 3
It works great, but is there a way to separate the code to find matches in two fields separately.

For example, Finding separate matches for KJ in one field and finding matches for KP in the next?

Someothing like this(still set to the in the above example) :
Code:
K   K
J    P
A   J
M  P
L   Z
P   K
J    K


Without referring to a specific column/field?


Top
 Profile  
 PostPosted: Fri Oct 03, 2014 1:19 pm   

Joined: Fri Oct 03, 2014 9:57 am
Posts: 5
That's some basic awk actually . You just have to write pattern-action couples in your code.

Code:
awk --assign pat="^[AJKM]\$" '
$2 ~ pat {print "case 1:" $0 }
$3 ~ pat { print "case 2:" $2} ' yourFile


Jean-Paul


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP