Grep and substr


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep and substr
# 1  
Old 11-09-2009
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
# 2  
Old 11-09-2009
try this ...

Code:
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

Last edited by protocomm; 11-09-2009 at 09:00 AM..
# 3  
Old 11-09-2009
thanks but returns blank.. i was thinking a substr wrapped around the grep but it doesnt run for me
# 4  
Old 11-09-2009
one other way also.,
Code:
echo 'PID : 25803 TID : 47983424956736PROC : db2sysc 1' | grep -E -o 'PID : [0-9]+' | grep -o -E '[0-9]+'

# 5  
Old 11-09-2009
Code:
echo "PID : 25803 TID : 47983424956736PROC : db2sysc 1" | awk '{print $3}'
25803

Or in one awk statement:
Code:
awk '/PID/{z=$3}END{print z}' ~/bug_tool.log
25803


Last edited by Scrutinizer; 11-09-2009 at 09:07 AM..
# 6  
Old 11-09-2009
Also, you can try the simple 'cut' command as used below -

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

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

Code:
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.
# 7  
Old 11-09-2009
Quote:
Originally Posted by ggs
Code:
grep PID ~/bug_tool.log | tail -1 | cut -d":" -f2 | tr -s " " ":" | cut -d":" -f1

all these can be done with one single awk command like what scrutinizer had posted.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk and substr

Hello All; I have an input file 'abc.txt' with below text: 512345977,213458,100021 512345978,213454,100031 512345979,213452,100051 512345980,213455,100061 512345981,213456,100071 512345982,213456,100091 512345983,213457,100041 512345984,213451,100011 I need to paste the first field... (10 Replies)
Discussion started by: mystition
10 Replies

2. UNIX for Dummies Questions & Answers

Substr

awk '/^>/{id=$0;next}length>=7 { print id, "\n"$0}' Test.txt Can I use substr to achieve the same task? Thanks! (8 Replies)
Discussion started by: Xterra
8 Replies

3. Shell Programming and Scripting

How to use if/else if with substr?

I have a command like this: listdb ID923 -l |gawk '{if (substr($0,37,1)==1 && NR == 3)print "YES" else if (substr ($0,37,1)==0 && NR == 3) print "NO"}' This syntax doesn't work. But I was able to get this to work: listdb ID923 -l |gawk '{if (substr($0,37,1)==1 && NR == 3)print "YES"}' ... (4 Replies)
Discussion started by: newbie2010
4 Replies

4. Shell Programming and Scripting

awk substr

HI I am using awk and substr function to list out the directory names in the present working directory . I am using below code ls -l | awk '{ if ((substr($1,1,1)) -eq d) {print $9 }}' But the problem is i am getting all the files and directories listed where as the requirement i wrote... (7 Replies)
Discussion started by: prabhu_kumar
7 Replies

5. Shell Programming and Scripting

Help with awk and substr

I have the following to find lines matching "COMPLETE" and extract parts of it using substr. sed -n "/COMPLETE/p" 1.txt | awk 'BEGIN { FS = "\" } {printf"%s %s:%s \n", substr($3,17,3),substr($6,4,1), substr($7,4,1)}' | sort | uniq > temp.txt Worked fine until the numbers in 2nd & 3rd substr... (5 Replies)
Discussion started by: zpn
5 Replies

6. UNIX for Dummies Questions & Answers

substr

can anybody explain this code? thanks in advance..:) (6 Replies)
Discussion started by: janani_kalyan
6 Replies

7. Shell Programming and Scripting

get substr?

Hi, I have a long string like, aabab|bcbcbcbbc|defgh|paswd123 dedededede|efef|ghijklmn|paswd234 ghghghghgh|ijijii|klllkkk|paswd345 lmlmlmmm|nononononn|opopopopp|paswd456 This string is devided into one space between substrings. This substrings are, aabab|bcbcbcbbc|defgh|paswd123... (6 Replies)
Discussion started by: syamkp
6 Replies

8. UNIX for Dummies Questions & Answers

Substr

Hi, My input file is 41;2;xxxx;yyyyy.... 41;2;xxxx;yyyyy.... 41;2;xxxx;yyyyy.... .. .. I need to change the second field value from 2 to 1. i.e., 41;1;xxxx;yyyyy.... 41;1;xxxx;yyyyy.... 41;1;xxxx;yyyyy.... .. .. Thanks in advance. (9 Replies)
Discussion started by: deepakwins
9 Replies

9. Shell Programming and Scripting

Using substr

What is the more efficient way to do this (awk only and default FS) ? $ echo "jefe@alm"|awk '{pos = index($0, "@");printf ("USER: %s\n",substr ($0,1,pos-1))}' USER: jefe Thx in advance (2 Replies)
Discussion started by: Klashxx
2 Replies

10. Shell Programming and Scripting

awk substr?

Sorry if this has been posted before, I searched but not sure what I really want to do. I have a file with records that show who has logged into my application: 2003-03-14:I:root: Log_mesg: registered servername:userid. (more after this) I want to pull out the userid, date and time into... (2 Replies)
Discussion started by: MizzGail
2 Replies
Login or Register to Ask a Question