Combine multiple awk commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combine multiple awk commands
# 1  
Old 06-14-2014
Combine multiple awk commands

Hi Team,

I am getting input like below

Code:
$ ps -ef | grep pmon | grep -v asm | grep -v grep
oracle    3246     1  0 00:03 ?        00:00:01 ora_pmon_racora1
oracle    4367     1  0 00:03 ?        00:00:01 ora_pmon_test1
oracle    6893     1  0 00:03 ?        00:00:01 ora_pmon_gipora1
oracle    2156     1  0 00:03 ?        00:00:01 ora_pmon_vitest1

I want the output like below. Currently i can acheive via two awk commands but is there any way can do by one command.

Code:
$ ps -ef | grep pmon | grep -v grep | grep -v asm | awk '{print $1}'
$ ps -ef | grep pmon | grep -v grep | grep -v asm | awk -F_ '{print $NF}'

Code:
expected output :
-----------------

oracle racora1
oracle test1
oracle gipora1
oracle vitest1

Pls advice

Regards
Kamal
# 2  
Old 06-14-2014
Try:
Code:
ps -ef | grep pmon | grep -v grep | grep -v asm | awk '
{	n = split($NF, u, /_/)
	print $1, u[n]
}'

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 06-14-2014
thanks perfectly working !
# 4  
Old 06-14-2014
Try :

Code:
$ ps -ef | grep pmon | grep -v grep | grep -v asm | awk -F'[ _]' '{print $1,$NF}'

Code:
$ ps -ef | grep pmon | grep -v grep | grep -v asm | awk '{s=$1;sub(/.*_/,x);print s,$1}'

These 2 Users Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 06-14-2014
Quote:
Originally Posted by Akshay Hegde
Try :

Code:
$ ps -ef | grep pmon | grep -v grep | grep -v asm | awk -F'[ _]' '{print $1,$NF}'

...
Excellent idea Akshay. Note, however, that some versions of awk sometimes (depending on field widths) use a tab as well as spaces as field delimiters. To be safe you might want to use:
Code:
awk -F'[ \t_]'

instead of:
Code:
awk -F'[ _]'

These 2 Users Gave Thanks to Don Cragun For This Post:
# 6  
Old 06-14-2014
If want to avoid the some of the pipes,

Code:
ps -ef | awk '/pmon/ && !/asm/ && !/grep/ {x=$1;gsub(/.*_/,_);print x FS $0}'

I guess, the process owner would be always oracle? as its an oracle process, may be you can fix that value if required (and acceptable).

Code:
ps -ef | awk '/pmon/ && !/asm/ && !/grep/ {gsub(/.*_/,_);print "oracle " $0}'

Regex's greedy match removes everything until the last "_".



Another version could be :
Code:
ps -ef | awk '/pmon/ && !/asm|grep/ {gsub(/.*_/,_);print "oracle " $0}'

This User Gave Thanks to clx For This Post:
# 7  
Old 06-14-2014
Thanks all for the valuable reply !!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combine two awk commands

Hi, Can someone please guide me how to combine the following two awk calls in one? I noticed that it is very often situation for me, and I think that it can be replaced with one awk call. The question is more general, not the exact one. echo "A B C/D" | awk '{print $3}' | awk -F/ '{print... (4 Replies)
Discussion started by: mirusnet
4 Replies

2. Shell Programming and Scripting

Combine multiple commands

I have the following sh-script: konsole -T todo -e vi todo.txt & konsole -T window1 -e ssh user@server & konsole -T window2 -e ssh user@server2 -e cd directory & The first two lines are working fine. The first opens a txt-file, the second opens a ssh-connection. The third line... (6 Replies)
Discussion started by: andre666
6 Replies

3. Shell Programming and Scripting

Combine awk commands into one

my code: gawk 'NR>'"${LASTLINENUM}"' && NR<='"${LINEENDNUM}"'' ${LOGFILE} | gawk '{l=$0;} /'"${STRING1}"'/ && /'"${STRING2}"'/ {for (i=NR-'"${BEFOREGLAF}"'; i<=NR+'"${AFTERGLAF}"'; i++) o=i; t++;} END { for(i=1; i<=NR; i++) if (o) print l; print t+=0;}' i would like to combine this into one... (5 Replies)
Discussion started by: SkySmart
5 Replies

4. Shell Programming and Scripting

Csh - how to combine multiple commands in one line

Hey everyone, I am working in an environment where the different users can use ksh or csh. My situation is that I need the same result with one single command line. I am searching for the real path the file is in. My ksh input and output ts2:ts2adm> cd $(dirname $(which sapcontrol)); pwd -P... (2 Replies)
Discussion started by: h1kelds
2 Replies

5. Shell Programming and Scripting

Pipe or combine output of three awk commands

What is the correct syntax to pipe or run three awk commands? Basically, using the output of the first awk as input in the second. Then using the output of the second awk in the third. Thank you :). awk 'FNR==NR {E; next }$3 in E {print $3, $5}' panel_genes.txt RefSeqGene.txt > update.txt |... (3 Replies)
Discussion started by: cmccabe
3 Replies

6. Shell Programming and Scripting

Awk: Combine multiple lines based on number of fields

If a file has following kind of data, comma delimited 1,2,3,4 1 1 1,2,3,4 1,2 2 2,3,4 My required output must have only 4 columns with comma delimited 1,2,3,4 111,2,3,4 1,222,3,4 I have tried many awk command using ORS="" but couldnt progress (10 Replies)
Discussion started by: mdkm
10 Replies

7. Shell Programming and Scripting

How to combine awk and bash commands in script ?

Dear friends, I am just trying write one script using 2 files one file will contain details like below #X SERVER X LOCATION URL="http://www.abcd.com" FILENAME="abc.txt" ID_NAME="myabc_xyz" SERVER_PATH="/usr/local/dummy/html/....." #Y SERVER Y LOCATION URL="http://www.xyz.com"... (10 Replies)
Discussion started by: Akshay Hegde
10 Replies

8. Shell Programming and Scripting

Combine multiple unique lines from event log text file into one line, use PERL or AWK?

I can't decide if I should use AWK or PERL after pouring over these forums for hours today I decided I'd post something and see if I couldn't get some advice. I've got a text file full of hundreds of events in this format: Record Number : 1 Records in Seq : ... (3 Replies)
Discussion started by: Mayday22
3 Replies

9. Shell Programming and Scripting

perform 3 awk commands to multiple files in multiple directories

Hi, I have a directory /home/datasets/ which contains a bunch (720) of subdirectories called hour_1/ hour_2/ etc..etc.. in each of these there is a single text file called (hour_1.txt in hour_1/ , hour_2.txt for hour_2/ etc..etc..) and i would like to do some text processing in them. Each of... (20 Replies)
Discussion started by: amarn
20 Replies

10. Shell Programming and Scripting

Combine two lists From Multiple Grep commands.

I'm working with a file with an xml structure. I'd like to parse it down to just the bits i want. Here is and example of the file <message id="96352877" method="status"> <date rfc="Sat, 12 Mar 2011 16:13:15 -0600" unix="1299967995" /> <services> <service id="facebook"... (4 Replies)
Discussion started by: Erulisseuiin
4 Replies
Login or Register to Ask a Question