35

I need to get a row based on column value just like querying a database. I have a command output like this:

Name  ID  Mem VCPUs      State   Time(s)

Domain-0   0  15485  16       r----- 1779042.1

prime95-01                512     1       -b----  61.9

Here I need to list only those rows where state is "r". Something like this,

Domain-0               0  15485  16       r----- 1779042.1

I have tried using "grep" and "awk" but still I am not able to succeed.

5 Answers 5

37

There is a variaty of tools available for filtering.

If you only want lines with "r-----" grep is more than enough:

command | grep "r-----"

Or

cat filename | grep "r-----"
Sign up to request clarification or add additional context in comments.

1 Comment

"Quotes are optional but might prevent grep tripping over the -'s" They are. But they won't.
5

grep can handle this for you:

yourcommand | grep -- 'r-----'

It's often useful to save the (full) output to a file to analyse later. For this I use tee.

yourcommand | tee somefile | grep 'r-----'

If you want to find the line containing "-b----" a little later on without re-running yourcommand, you can just use:

grep -- '-b----' somefile

I recommend putting -- after your call to grep since your patterns contain minus-signs and if the minus-sign is at the beginning of the pattern, this would look like an option argument to grep rather than a part of the pattern.

2 Comments

Which part doesn't work? It did when I wrote it nearly seven years ago but I'm happy to update it if you can point out the error.
+1 for the -- hint. pip list --help | grep -- "--user" works, pip list --help | grep "--user" doesn't.
3

try:

awk '$5 ~ /^r.*/ { print }' 

Like this:

cat file | awk '$5 ~ /^r.*/ { print }' 

Comments

3

grep solution:

command | grep -E "^([^ ]+ ){4}r"

What this does (-E switches on extended regexp):

The first caret (^) matches the beginning of the line. [^ ] matches exactly one occurence of a non-space character, the following modifier (+) allows it to also match more occurences.

Grouped together with the trailing space in ([^ ]+ ), it matches any sequence of non-space characters followed by a single space. The modifyer {4} requires this construct to be matched exactly four times.

The single "r" is then the literal character you are searching for.

In plain words this could be written like "If the line starts <^> with four strings that are followed by a space <([^ ]+ ){4}> and the next character is , then the line matches."

A very good introduction into regular expressions has been written by Jan Goyvaerts (http://www.regular-expressions.info/quickstart.html).

Comments

0

Filtering by awk cmd in linux:-

Firstly find the column for this cmd and store file2 :-

awk '/Domain-0 0 15485 /' file1 >file2

Output:-

Domain-0 0 15485 16 r----- 1779042.1

after that awk cmd in file2:-

awk '{print $1,$2,$3,$4,"\n",$5,$6}' file2

Final Output:-

Domain-0 0 15485 16

r----- 1779042.1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.