awk and basename


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk and basename
# 1  
Old 11-23-2011
awk and basename

im trying to extract the basename of a process running on a host
processx is running at host1 as /applications/myapps/bin/processx

i wanted to check if its running, then extract the basename only using:
Code:
$ ssh host1 "ps aux | grep -v 'grep' | grep 'processx'" | awk '{ print basename $11}'

unfortunately is still get the whole path+filename (ie /applications/myapps/bin/processx)
is basename not supposed to run in awk?

thanks!
# 2  
Old 11-23-2011
Use a substitute command to replace all characters from the beginning of the string to right most slash, then print it:

Code:
awk '{ print gensub( ".*/", "", $11 );  }'

---------- Post updated at 23:50 ---------- Previous update was at 23:45 ----------

You can also eliminate the grep -v if you use this syntax:

Code:
grep '[p]rocessx'

It will match processx, but won't match grep [p]rocessx which will appear in the ps listing.

Code:
ssh host1 "ps aux | grep '[p]rocessx'"  | awk ...

This User Gave Thanks to agama For This Post:
# 3  
Old 11-23-2011
Quote:
Originally Posted by agama
Code:
ssh host1 "ps aux | grep '[p]rocessx'"  | awk ...

this is a neat trick.. thanks!

Quote:
Originally Posted by agama
Use a substitute command to replace all characters from the beginning of the string to right most slash, then print it:

Code:
awk '{ print gensub( ".*/", "", $11 );  }'

now back to my problem. the above works, but not in what i'm trying to do. let me clarify...

Code:
ssh_cmd = "ssh $host "ps aux | grep -v 'grep' | grep '$process'" | awk '{ if ($11 == "$process") print $9; }'" ;

where "$process" is any user input process
code is supposed to check if $process is running. however, if said process is run with path, it is fooled to believe that it's not.
# 4  
Old 11-23-2011
Like this?
Code:
ssh_cmd="ssh $host \" ps aux | grep -v grep | grep $process | awk -F/ '{print $NF}' \" ";

--ahamed
# 5  
Old 11-23-2011
Quote:
Originally Posted by ahamed101
Like this?
Code:
ssh_cmd="ssh $host \" ps aux | grep -v grep | grep $process | awk -F/ '{print $NF}' \" ";

--ahamed
ahamed you must escape with a backslash for pass to awk that as a paramater
Code:
{print \$NF}

and my way
Code:
ssh_cmd="ssh $host ps aux|awk '{if(\$11==\"$process\") print \$9}'"

regards
ygemici
# 6  
Old 11-23-2011
Quote:
Originally Posted by ygemici
Code:
ssh_cmd="ssh $host ps aux|awk '{if(\$11==\"$process\") print \$9}'"

i
I believe that the user will be entering foo and $11 may contain /path/to/foo which needs to be 'basenamed' before comparing. A slight change:

Code:
ssh_cmd="ssh $host ps aux|awk '{if( gensub( \".*/\", \"\", \$11 ) == \"$process\") print \$9}'"

# 7  
Old 11-23-2011
confused here a bit. I'm not grepping the $process anymore before I awk?
that could mean the script would check all process output of ps aux right?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

$(basename $0)

what is the meaning of "script_name=$(basename $0)", can someone please explain? (1 Reply)
Discussion started by: abhi200389
1 Replies

2. Shell Programming and Scripting

awk & basename puzzler - advise sought

Hi I have been able generate a file ($ELOG) that can have multiple lines within it. The first column represents the full path source file and the other is the full path target ... the file names are the same but the target directory paths are slightly different. <source_dir1>/file1 ... (4 Replies)
Discussion started by: davidra
4 Replies

3. Shell Programming and Scripting

Difference between $0 and basename

Hi, Could you please help me to know the difference between $0 and basename in unix how they useful in shell scripting. Thanks in advance (3 Replies)
Discussion started by: lnviyyapu
3 Replies

4. UNIX for Dummies Questions & Answers

basename

Hi, can anyone let me know how to interpret the below third line in the following code. Gone through the man pages of "basename", but no go. for f in *.foo; do base=`basename $f .foo` mv $f $base.bar done Thanks. (2 Replies)
Discussion started by: venkatesht
2 Replies

5. Shell Programming and Scripting

extracting basename in awk or nawk

I am having a hard time extracting the file name from the above code. Instead of printing /folder/file.1$.5$, I would like it to print the file name file.1$.5$. I have tried using basename but it looks like NAWK or AWK does not recognise basename. Each time I type it in, it prints out the word... (4 Replies)
Discussion started by: asemota
4 Replies

6. Shell Programming and Scripting

getting basename inside awk script

hi if we have to use basename how can we do this in awk? did the below but is not working.. psg -t "?"| awk '{ command=($5 ~ /^/)? $9:$8 # cmd_name=`basename $command` (gives error) system("basename $command >>... (10 Replies)
Discussion started by: Anteus
10 Replies

7. Shell Programming and Scripting

basename $0

hi, can anyone help me by saying what is basename.. i have seen this in many programs where the basename is used.... thanks, Krips (4 Replies)
Discussion started by: kripssmart
4 Replies

8. Shell Programming and Scripting

basename problem

Hi guys if i do a=`basename -e -s /home/j/john/*` du -k -h $a | sort -nr | head -10 why when i run the script does it work but also say usage basename string any ideas thanks (9 Replies)
Discussion started by: musicmancanora4
9 Replies

9. Shell Programming and Scripting

Using find command with awk or basename

Hi, I am using the following command to extract any log files that are older than 3 days using the following command. find DIR/LOGDIR -type f -mtime +3 |grep LOG > log_list.out The results are DIR/LOGDIR/1.LOG DIR/LOGDIR/2.LOG DIR/LOGDIR/3.LOG DIR/LOGDIR/4.LOG How do inculde (basename... (4 Replies)
Discussion started by: sam_78_nyc
4 Replies

10. Shell Programming and Scripting

reverse of basename

Hi, Can someone let me know how to find the reverse of the basename i.e i have /apps/tiv/pmon/xxxx.dat and I want /apps/tiv/pmon/ Thanks (7 Replies)
Discussion started by: braindrain
7 Replies
Login or Register to Ask a Question