0

I have file which context is as follows:

               CA Services Status Report

           Component Name               Pid        Status
------------------------------------  -------  --------------
WAAE Web Server (ASD)                   20841  running
WAAE Application Server (ASD)           20281  running
WAAE Scheduler (ASD)                    20486  running
WAAE Agent (WA_AGENT)                   20109  running
CA-wcc-services Server                  22385  running
CA-wcc Server                           24410  running

And i wanted to print only Pids on the screen.

I tried using awk and grep but unable to print Pids.

cat test | awk '{print $5}'

I want to print only the Pid column but unable to print it exactly.

1
  • 1
    Are your columns tab separated or just spaces? Commented Oct 11, 2019 at 6:16

4 Answers 4

5

Could you please try following(tested with provided samples).

awk 'NF && !/Report/ && !/Status/ && !/^-/{print $(NF-1)}' Input_file

OR(as per James sir's comment):

awk 'NF&&$(NF-1)~/[0-9]+/{print $(NF-1)}' Input_file
Sign up to request clarification or add additional context in comments.

3 Comments

$(NF-1) is the best. Maybe reduce it to: awk 'NF&&$(NF-1)~/[0-9]+/{print $(NF-1)}' file
Clever use of $(NF-1) ++
@RavinderSingh Thanks for the your inputs. Please let me know what should we do if need to display only third column i.e. status of services. just like running or not running.
2

If you only have digits in the pid field, extract the digits:

$ awk 'match($0,/[0-9]+/){print substr($0,RSTART,RLENGTH)}' file
20841
20281
...

Or use more-than-one space as field-separator after record 4:

$ awk -F"  +" 'NR>=5{print $2}' file
20841
20281
...

Comments

1

You may use this awk assuming file has only spaces and digits may appear anywhere in a line:

awk '!n && n=index($0, " Pid "){++n} n{s=substr($0, n); sub(/ .*/, "", s); print s}' file

Pid
-----
20841
20281
20486
20109
22385
24410

Comments

1

Since you seem to have a fixed layout, just measure where things are:

<file tail +5 a.txt | cut -c41-45

Start with 5th line, take only characters from 41th to 45th column. Same with AWK (though I find the above more intuitive):

awk 'NR>=5 { print substr($0, 41, 5) }' file

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.