?
Solved

linux command to match text in columns

Posted on 2011-05-08
12
Medium Priority
?
311 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:andieje
  • 6
  • 5
12 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 35716981
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
 
LVL 31

Accepted Solution

by:
farzanj earned 1332 total points
ID: 35717020
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
 
LVL 38

Assisted Solution

by:wesly_chen
wesly_chen earned 668 total points
ID: 35717154
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:andieje
ID: 35717185
How do you get it to match exactly one character as [A-Z]{1} didnt work
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35717189
Well, you don't need anything after it
Just [A-Z]
0
 

Author Comment

by:andieje
ID: 35717196
some of the columns have >1 character and I don't want to match those
0
 

Author Comment

by:andieje
ID: 35717199
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
 
LVL 31

Expert Comment

by:farzanj
ID: 35717201
Please give example what you would want to match.  I will give you expression you need.
0
 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 1332 total points
ID: 35717205
Try $2 ~ /^[A-Z]$/ && $4 ~ /^[A-Z]$/
0
 

Author Comment

by:andieje
ID: 35717207
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
 

Author Comment

by:andieje
ID: 35717208
that did it
0
 

Author Closing Comment

by:andieje
ID: 35717212
thank you
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month16 days, left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question