awk - grep particular word from output

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - grep particular word from output
# 1  
Old 10-03-2013
awk - grep particular word from output

Hi Experts,

- Getting error while using it through a variable to get the PID,

# UNIX95=1 ps -e -o pcpu,pid,ppid,stime,etime,args | awk '{if ($2~"^42$") print $0}'
 0.00    42     0  Feb 10  600-17:21:29 nfs_async_io

- But when using with a variable it is not working .
# UNIX95=1 ps -e -o pcpu,pid,ppid,stime,etime,args | awk -v pid1=$PID '{if ($2~^pid1$) print $0}'
 syntax error The source line is 1.
 The error context is
                {if >>>  ($2~^ <<< pid1$) print $0}
 awk: The statement cannot be correctly parsed.
 The source line is 1.

please advise what is wrong happening..

Last edited by joeyg; 10-03-2013 at 04:58 PM.. Reason: moved code tags, changed "lime" to "green"
# 2  
Old 10-03-2013
Try ($2==pid1)

EDIT: Or, maybe, ($2 ~ "^"pid1"$") ?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 10-03-2013


I suppose there is a good reason to do this but, why not just use the the switich to specify the specific something like

ps -T ${PID}

and the -o options you are interested in.
# 4  
Old 10-03-2013
Hi blackrageous,

It did not work ,

$ ps -T ${PID}
ps: illegal option -- T
usage: ps [-edaxzflP] [-u ulist] [-g glist] [-p plist] [-t tlist] [-R prmgroup] [-Z psetidlist]

This is ksh on hp-ux 11.23. Please advise,
# 5  
Old 10-03-2013
It's -p for proc list (which I assume was the aim).
This User Gave Thanks to CarloM For This Post:
# 6  
Old 10-03-2013
UNIX95=1 ps -p "$PID" -o pcpu,pid,ppid,stime,etime,args

-edit- OK, I see CarloM already answered this one..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 10-03-2013
RudiC thanks, worked the first one.
CarolM & Scrutinizer, thanks worked too... ,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep or awk a unique and specific word across many fields

Hi there, I have data with similar structure as this: CHR START-SNP END-SNP REF ALT PATIENT1 PATIENT2 PATIENT3 PATIENT4 chr1 69511 69511 A G homo hetero homo hetero chr2 69513 69513 T C . hetero homo hetero chr3 69814 69814 G C . . homo homo chr4 69815 69815 C A hetero . . hetero is... (10 Replies)
Discussion started by: daashti
10 Replies

2. Shell Programming and Scripting

Grep output to awk command

Hi Team(Solaris 5.8/Ksh), How can we save grep output to awk variable when grep returns more than one line or word. abc.log # more abc.log Hi Makarand How r u bye Makarand Hello when grep returns only 1 word below command works nawk -v var=`cat abc.log |grep "Hello"` 'BEGIN { if... (6 Replies)
Discussion started by: Makarand Dodmis
6 Replies

3. Shell Programming and Scripting

awk or grep to search one column and output the other

Hello, it would be great if someone can help me with the following: I want to search for the rows from fileA in column 1 of fileB and output column 2 of fileB if found in fileC. In the moment I search within the complete file. How can I change the code so only column 1 is searched? cat fileA... (7 Replies)
Discussion started by: Manyaka
7 Replies

4. Shell Programming and Scripting

Formatting grep and awk output

Hi there. I have a very large file and I am trying to format it so that I can pull out certain pieces of data/info and report it in spreadsheet format/style. The file has ###### which will separate each line that will be listed in the spreadsheet. Whenever I find "No" at the end of a line I want... (7 Replies)
Discussion started by: kieranfoley
7 Replies

5. Shell Programming and Scripting

Selecting awk output depending on grep result

Hi, I don't script often enough to know how to do this, and I can't seem to find a right example online. I have a csv output from an old, old system (Win2K???), from which I want to extract only certain fields. Initially I came up with something like this: cat file1 | awk -F '"' '{print $8... (7 Replies)
Discussion started by: takada
7 Replies

6. Shell Programming and Scripting

Grep to isolate a text file line and Awk to select a word?

I am looking at using grep to locate the line in the text file and them use awk to select a word or words out of it. I know awk needs -v to allow a variable to be used, but also needs -F to allow the break up of the sentence and allow the location of separate variables. $line = grep "1:" File |... (8 Replies)
Discussion started by: Ironguru
8 Replies

7. Shell Programming and Scripting

awk & grep - check for a value and write sub-word

Hi people, I have a file status.txt: Following 6 ports are totally or partially unavailable: ------------------------------------------------------------ MOD LINK PORTNAMES STAT1 STAT2 STAT3 SYN TYPE ------------------------------------------------------------ 8 Pr37 ... (12 Replies)
Discussion started by: gc_sw
12 Replies

8. Shell Programming and Scripting

How to grep/awk/egrep two values for given output?

Dear Friends, I have a command which can result following output. Packet is: /var/adm/yyyy/pkt6043 Intended for network : /vob/repo I would like to retrive pkt6043 and /vob/repo using single command. Blue color test will be always contstant and red color text will be dynamic ... (2 Replies)
Discussion started by: baluchen
2 Replies

9. Shell Programming and Scripting

pipe'ing grep output to awk

This script is supposed to find out if tomcat is running or not. #!/bin/sh if netstat -a | grep `grep ${1}: /tomcat/bases | awk -F: '{print $3}'` > /dev/null then echo Tomcat for $1 running else echo Tomcat for $1 NOT running fi the /tomcat/bases is a file that... (2 Replies)
Discussion started by: ziggy25
2 Replies

10. Shell Programming and Scripting

GREP:Output all lines containing word

Output all lines in the file temp that contain the word dog using GREP only and in one line!!! I tried grep ']dog]' temp but it doesnt catch word dog when is at beginning or end, like: Our dog is nice /this OK Nice dog /this NOT dog good /this NOT Thank... (3 Replies)
Discussion started by: ljubayuu
3 Replies
Login or Register to Ask a Question