linux command to match text in columns

Hi

How would i use sed or awk to get me the lines in a file that match regular expressions in columns 2 and 4? The columns are separated by tabs

The regexp for both columns is just a basic character from A-Z e.g. [A-Z]

many thanks
andiejeAsked:
Who is Participating?
 
farzanjCommented:
Sorry again

With sed
sed -n "/.*\t[A-Z][A-Z]*\t.*\t[A-Z][A-Z]*/p" filename

Open in new window


And with awk
 
awk -F'\t' '$1 ~ /[A-Z]+/ && $3 ~ /[A-Z]+/' filename

Open in new window

0
 
farzanjCommented:
Here is a sed command for you
sed -n "/.*\t[A-Z][A-Z]*\t.*\t[A-Z][A-Z]*/p"

Open in new window

0
 
wesly_chenCommented:
Hi farzanj, for awk, column 2 and 4 should be $2 and $4.
a basic character doesn't need +
      
awk -F'\t' '$2 ~ /[A-Z]/ && $4 ~ /[A-Z]/ {print}' filename
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
andiejeAuthor Commented:
How do you get it to match exactly one character as [A-Z]{1} didnt work
0
 
farzanjCommented:
Well, you don't need anything after it
Just [A-Z]
0
 
andiejeAuthor Commented:
some of the columns have >1 character and I don't want to match those
0
 
andiejeAuthor Commented:
i also tried $2 == /[A-Z]/ && $4 == /[A-Z]/ to make sure i got the columns just 1 character long and that didnt work
0
 
farzanjCommented:
Please give example what you would want to match.  I will give you expression you need.
0
 
farzanjCommented:
Try $2 ~ /^[A-Z]$/ && $4 ~ /^[A-Z]$/
0
 
andiejeAuthor Commented:
I want columns 2 and 4 to be just a single character from A-Z. I checked the length of the columns i want to match and they are indeed one character, no hidden characters but the expressions i have tried did not work.

i'd like to use the awk example rather than sed
0
 
andiejeAuthor Commented:
that did it
0
 
andiejeAuthor Commented:
thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.