how to using variables in search pattern in awk script - bash

I want to print the pid when finding matched process while the match pattern is inputted:
ps aux | awk -v in="$1" '/in/{print $1}'
It seems the former awk sentence is not right. After checking many results in google like this, I change my script in the following but still cannot work:
ps aux | awk -v in="$1" '/$0 ~ in/{print $1}'
or
ps aux | awk -v in="$1" '($0 ~ in) {print $1}'

You are fairly close in all your attempts. Problem is that in is a reserved keyword in awk.
You can use:
ps aux | awk -v var="$1" '$0 ~ var {print $1}'
Or else non-regex way:
ps aux | awk -v var="$1" 'index($0, var) {print $1}'

Related

How to grep first match and second match(ignore first match) with awk or sed or grep?

> root# ps -ef | grep [j]ava | awk '{print $2,$9}'
> 45134 -Dapex=APEC
> 45135 -Dapex=JAAA
> 45136 -Dapex=APEC
I need to put the first APEC of first as First PID, third line of APEC and Second PID and last one as Third PID.
I've tried awk but no expected result.
> First_PID =ps -ef | grep [j]ava | awk '{print $2,$9}'|awk '{if ($0 == "[^0-9]" || $1 == "APEC:") {print $0; exit;}}'
Expected result should look like this.
> First_PID=45134
> Second_PID=45136
> Third_PID=45135
With your shown samples and attempts please try following awk code. Written and tested in GNU awk.
ps -ef | grep [j]ava |
awk '
{
val=$2 OFS $9
match(val,/([0-9]+) -Dapex=APEC ([0-9]+) -Dapex=JAAA\s([0-9]+)/,arr)
print "First_PID="arr[1],"Second_PID=",arr[3],"Third_PID=",arr[2]
}
'
How about this:
$ input=("1 APEC" "2 JAAA" "3 APEC")
$ printf '%s\n' "${input[#]}" | grep APEC | sed -n '2p'
3 APEC
Explanation:
input=(...) - input data in an array, for testing
printf '%s\n' "${input[#]}" - print input array, one element per line
grep APEC - keep lines containing APEC only
sed -n - run sed without automatic print
sed -n '2p' - print only the second line
If you just want the APECs first...
ps -ef |
awk '/java[ ].* -Dapex=APEC/{print $2" "$9; next; }
/java[ ]/{non[NR]=$2" "$9}
END{ for (rec in non) print non[rec] }'
If possible, use an array instead of those ordinally named vars.
mapfile -t pids < <( ps -ef | awk '/java[ ].* -Dapex=APEC/{print $2; next; }
/java[ ]/{non[NR]=$2} END{ for (rec in non) print non[rec] }' )
After read from everyone idea,I end up with the very simple solution.
FIRST_PID=$(ps -ef | grep APEC | grep -v grep | awk '{print $2}'| sed -n '1p')
SECOND_PID=$(ps -ef | grep APEC | grep -v grep | awk '{print $2}'| sed -n '2p')
JAWS_PID=$(ps -ef | grep JAAA | grep -v grep | awk '{print $2}')

Error when awk command's parameter is being used in pattern

I'm running these commands:
ps -ef | awk -v piddd="$child_pid" '$2 ~ /\<piddd\>/ { print $3; }'
ps -ef | awk -v piddd="$child_pid" '$2 ~ /piddd/ { print $3; }'
It doesn't give me any result. When I try with this one, I get what I need, although in some cases I'll get additional pids:
ps -ef | awk -v piddd="$child_pid" '$2 ~ piddd { print $3; }'
What is wrong with first ones?
You cannot use a variable with the /pattern/ syntax.
If you want to add word boundaries (and your version of awk supports the syntax), you can do so by concatenating strings:
ps -ef | awk -v piddd="$child_pid" '$2 ~ "\\<" piddd "\\>" { print $3 }'
Note that the \ must be escaped in this case.
If you just want the whole field to match the exact variable, I'd suggest using a simple string comparison:
ps -ef | awk -v piddd="$child_pid" '$2 == piddd { print $3 }'
There isn't a need to parse the output of ps utility just to get the parent pid PPID of a child PID. The ps utility already provides this functionality.
ps -o ppid= -p $child_pid
The parameter -o ppid= tells ps to just print the parent pid. Without = the printout will contain a header PPID.
The parameter -p $child_pid tells ps to get the process information from the process id identified by variable $child_pid.
ps -ef | awk '$2 ~ /'`echo "$child_pid"`'/ { print $3; }'
# or
ps -ef | awk '$2 ~ /'$child_pid'/ { print $3; }'
# or create function
function father_pid() { ps -ef | awk '$2 ~ /'$1'/ { print $3; }'; }
# use function
father_pid $child_pid

Bad substitution using awk

I am trying to open some files as awk's output; the command is:
grep "formatDate\s=" "js/components/" | awk '{print $1}' | awk -F ":" '/1/ {print $1}'
and it (seems to) work correctly.
If I try to open that output as vim's tabs, like this:
vim -p ${ grep "formatDate\s=" "js/components/" | awk '{print $1}' | awk -F ":" '/1/ {print $1}' }
then I get:
-bash: ${ grep "formatDate\s=" "js/components/" | awk '{print $1}' | awk -F ":" '/1/ {print $1}' }: bad substitution
Any help? Thanks.
The way to execute a command is $(), whereas you are using ${}.
Hence, this should work:
vim -p $(grep "formatDate\s=" "js/components/" | awk '{print $1}' | awk -F ":" '/1/ {print $1}')

How to escape grep and awk within pipe in an alias?

I want to create an alias for an long command. But I'm not able to escape it correct, I guess it's a problem with the pipes.
My original command
ps aux | grep gimp | awk '{ print $2 '\011' $11 }' | grep -v 'grep'
My attempt for an alias
alias psa="ps aux | grep $1 | awk '{ print \$2 \"\011\" \$11 }' | grep -v 'grep'"
But I get an error that grep can not open file foo (when I do psa foo)
When I remove the last part | grep -v 'grep' then awkthrows the same error.
I prefer an alias before an shell script.
You need to use a function if you want to to insert arguments:
psa() {
ps aux | grep "$1" | awk '{print $2 "\t" $11 }' | grep -v grep
}
You can avoid all the escaping by using a function for this:
myps() {
ps aux | grep gimp | awk '{ print $2 "\011" $11 }' | grep -v 'grep'
}

How to correct this awk code without eval?

The following piece of awk code works fine if I use eval builtin
var='$1,$2'
ps | eval "awk '{print $var}'"
But when I try to knock off eval and use awk variable as substitute then I am not getting the expected result
ps | awk -v v1=$var '{print v1}' # output is $1,$2
ps | awk -v v1=`echo $var` '{print v1}' # output is same as above
ps | awk -v v1=$var '{print $v1}' # output is all the fields of ps command
ps | eval "awk -v v1=$var '{print v1}'" # output is column of comma
How to get the desire output without using eval?
Use double-quotes in the awk command:
ps | awk "{print $var}"

Resources