Grep and substr

Hi,

A quick one, I abstract the last line from a file which hasa string PID in itie.

grep PID ~/bug_tool.log | tail -1

result
PID : 25803 TID : 47983424956736PROC : db2sysc 1

but any ideas on the best way to grab only the string 25803

Thanks

try this ...

grep PID ~/bug_tool.log | tail -1 | awk -F "[: ]" '{print $4}' 

it depends of the blank before the word PID, try to replace $4 by $5

thanks but returns blank.. i was thinking a substr wrapped around the grep but it doesnt run for me

one other way also.,

echo 'PID : 25803 TID : 47983424956736PROC : db2sysc 1' | grep -E -o 'PID : [0-9]+' | grep -o -E '[0-9]+'
echo "PID : 25803 TID : 47983424956736PROC : db2sysc 1" | awk '{print $3}'
25803

Or in one awk statement:

awk '/PID/{z=$3}END{print z}' ~/bug_tool.log
25803

Also, you can try the simple 'cut' command as used below -

grep PID ~/bug_tool.log | tail -1 | cut -d":" -f2

That would return - 25803 TID. To cut out TID you can use

grep PID ~/bug_tool.log | tail -1 | cut -d":" -f2 | tr -s " " ":" | cut -d":" -f1

tr -s " " ":" - give a space in between the first quotes.

all these can be done with one single awk command like what scrutinizer had posted.

great thanks scrutinizer

---------- Post updated at 09:29 AM ---------- Previous update was at 09:05 AM ----------

great thanks scrutinizer