1836462 Members
2712 Online
110101 Solutions
New Discussion

Re: awk help required.

 
SOLVED
Go to solution
Archana1
Frequent Advisor

awk help required.

Hello experts,


ps -ef | grep 4444

weblogic 24185 1 0 Feb 21 ? 0:00 /bin/sh ./startNodeManager.sh rodyserver 4444

From above command I want to capture last three coulmns of the output.

using awk `{print $NF}` I can capture only last column. But I need last three columns. Please help ?


12 REPLIES 12
rariasn
Honored Contributor

Re: awk help required.

Hi,

ps -ef|grep 4444|grep -v grep | awk '{print $10, $11, $12}'

rgs,
James R. Ferguson
Acclaimed Contributor
Solution

Re: awk help required.

Hi:

# X="weblogic 24185 1 0 Feb 21 ? 0:00 /bin/sh ./startNodeManager.sh rodyserver 4444"

# echo ${X}|awk '/ 4444$/ {print $(NF-2),$(NF-1),$NF}'
./startNodeManager.sh rodyserver 4444

This also eliminates the 'grep' since 'awk' does the matching too --- here specifically anchored to the end of the line.

Regards!

...JRF...
Raj D.
Honored Contributor

Re: awk help required.

x={'/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'} ;ps -ef | grep 4444 | grep -v grep |sed $x| awk '{print $3,$2,$1}'|sed $x #Cheers.
" If u think u can , If u think u cannot , - You are always Right . "
James R. Ferguson
Acclaimed Contributor

Re: awk help required.

Hi (again):

> Raj: I'm sorry, but you have to be kidding. The OP wants to capture the last three columns of the output. What's so hard about that?

Pipelines where 'grep', 'sed' and 'awk' are mixed together are red-flags for code that begs to be re-worked. All three filters can perform matching operations. Why spawn multiple processes when one will suffice? Why read and write the same data down the pipeline spawning new processes as you go?

Regards!

...JRF...
Raj D.
Honored Contributor

Re: awk help required.

James,
thanks, seems to be kidding but I am not,
This is a simple statement but looks complex :) as there is no rev command in hp-ux, else it would have been looked simple: (sed simulating the rev here).

$ ps -ef|grep 4444|rev| awk '{print $3,$2,$1}'|rev





Btw, question to Archana,
what is the problem getting 10th/11th & 12th field with awk. ? (Those are last three field).

Also Archana would you mind assigning point at the end? This is not only me, many people has advised the same in many of your thread.!!


" If u think u can , If u think u cannot , - You are always Right . "
James R. Ferguson
Acclaimed Contributor

Re: awk help required.

Hi (again):

> Raj: as there is no rev command in hp-ux, else it would have been looked simple: (sed simulating the rev here).

Yes, and you have to do it *twice* to put the data back into its original order, adding another process and more I/O.

While Linux has 'rev', Perl has 'reverse()':

# X="weblogic 24185 1 0 Feb 21 ? 0:00 /bin/sh ./startNodeManager.sh rodyserver 4444"

# echo $X|perl -nle 'print scalar reverse $_'
4444 revresydor hs.reganaMedoNtrats/. hs/nib/ 00:0 ? 12 beF 0 1 58142 cigolbew

# echo $X|perl -nle '@a=split;print join " ",reverse @a'
4444 rodyserver ./startNodeManager.sh /bin/sh 0:00 ? 21 Feb 0 1 24185 weblogic

Regards!

...JRF...
Raj D.
Honored Contributor

Re: awk help required.

I like the last one using array, thats great, t u again..

(0 pts. for this pls)
" If u think u can , If u think u cannot , - You are always Right . "
Archana1
Frequent Advisor

Re: awk help required.

Now Iam looking for an statement that Incase particular entry (red...) doesn't exists in dynamic log file then write into other log file.

The log file contains :-
Red
Yellow
Green

Pls help out ?
Raj D.
Honored Contributor

Re: awk help required.


You didn't yet assign the points to those who helped you on time, and answered with their valuable times here.

I hope you review the threads after posting, Don't you.?



Your Profile says:

" I have assigned points to 17 of 114 responses to my questions. "

- That means you have not appreciated by assigning points to (114-17) = 97 responses and seems you just dis-respected those answers. Does that make sense!.




- Again it is advised to read the Forum rules and courtesy pages before you start posting.

How do I assign points? :
http://forums11.itrc.hp.com/service/forums/helptips.do?#33



How many points should I assign to replies?
http://forums11.itrc.hp.com/service/forums/helptips.do?#34






Finally:
Here is how you can assign points to all your thread you have ignored so far:

http://forums13.itrc.hp.com/service/forums/pageList.do?forumId=1&&listType=unassigned&userId=WW286726



The Forums community appreciates your efforts to fairly rate the answers you have received.
" If u think u can , If u think u cannot , - You are always Right . "
James R. Ferguson
Acclaimed Contributor

Re: awk help required.

HI (again):

> Now Iam looking for an statement that Incase particular entry (red...) doesn't exists in dynamic log file then write into other log file.

I presume that you want the list of filenames in which the token doesn't appear. One simple way is:

# grep -v red * /dev/null|awk -F: '{print $1}'|sort -u > myresults

...which looks at all files in your current directory that do *not* contain the token "red". Lines in each file that fail to match are output with the filename and a colon (":") separator before the line content. We snip out this name and reduce the population to a unique list.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: awk help required.

>JRF: I presume that you want the list of filenames in which the token doesn't appear.
# grep -v red * /dev/null | awk -F: '{print $1}' | sort -u

This isn't going to do that. It's going to list all files that have lines that don't have red.
You need something like:
for file in *; do
grep -q -w red $file
if [ $? -ne 0 ]; then
echo "$file" # no scummy red, nowhere
fi
done > ../myresults

>fail to match are output with the filename and a colon

-l already does that.

If you don't want to invoke grep in a loop, you can use vector methods and do grep -l all all files then use comm(3) with ls(1) to weed out the the reds.
James R. Ferguson
Acclaimed Contributor

Re: awk help required.

Hi (again):

> Dennis: >JRF: I presume that you want the list of filenames in which the token doesn't appear...This isn't going to do that.

Yes, you are correct of course. I'm not sure what I was thinking and my quick test was a shambles of rigor :-)

A one-liner might be"

# grep -c -w red *|awk -F: '$2==0 {print $1}'

...which would show the filenames *not* containing the "word" 'red'.

Regards!

...JRF...