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}"
Related
My code is :
#!/bin/bash
strversion=`apache2ctl -v | awk '{print $3}' | sed 's/(Debian)//g;s/Server//g;s/built//g;s/2022-06-09T04:26:43//g'`
echo ${strversion%}
exit 0
i get this:
Apache/2.4.54
but i will have to look
Apache version 2.4.54
That's because the variable expansion is not quoted. An unquoted variable is subject to Word Splitting (and Filename Expansion)
echo "Apache2 - ${strversion%')'}"
# ...^...........................^
See also Security implications of forgetting to quote a variable in bash/POSIX shells
Try the following:
strversion=$(apache2ctl -v | head -1 | awk -F '[ /]' '{printf "%s version %s", $3, $4}')
i have "handler: xyz.lambda_handler" text in one file and i want "xyz.lambda_handler" i.e text present next to "handler:" as output using shell script, how can i do this.
I have tried
awk -F '${handler}' '{print $1}' filename | awk '{print $2}
grep handler filename
command but not getting correct output
as mentioned in qtn.
I combined two commands and i got my answer
grep Handler: filename | awk -F '${handler}' '{print $1}' | awk '{print $2}'
grep givepattern givefilename | awk -F '${givepattern}' '{print $1}' | awk '{print $2}'
It's grep, not greap. To print only the matched parts of a matching line, use option -o.
grep -o xyz.lambda_handler filename
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}'
Problem Description
In my Unix ksh system, I am having an environment variable A on doing
echo ${A}
I am getting the desired value which is hello.
I can check this value in
env | grep ${A}
output: A=hello
or in
printenv | grep ${A}
output: A=hello
Now I have a file file which contains the list of environment variables and I have to fetch the corresponding value.
Now what I tried just for only first value of the file.
env | grep $(cat file | awk 'NR==1{print $1}') --shows nothing
b=$(cat file | awk 'NR==1{print $1}')
env | grep echo $b
b=cat TEMP_ES_HOST_MAP | awk -F"|" 'NR==1{print $2 }'
echo $b
c=eval $b
c=echo $b
Nothing seems to be working.
Thank you
You can use xargs:
awk -F '[$()]+' '{print $1$2}' envfile | xargs printenv
Where:
cat envfile
$(LANG)
$LINES
USER
HISTFILESIZE
If you are looking for the variable named A in the output from env and printenv then using grep ${A} is incorrect (and unsafe/does not work for variables of more than one word).
What you want for that is:
env | grep ^A=
printenv | grep ^A=
So assuming your file looks like this:
VAR1
VAR2
OTHER_VAR
and you want to search for those you could use something like this (assuming you have process substitution):
env | grep -f <(awk '{print "^"$0"="}' varfile)
Assuming you don't have process substitution (or you would rather a simpler solution, I do) you can do this:
env | awk -F = 'NR==FNR{a[$1]++; next} $1 in a' varfile -
Actually this should work too and is even simpler (assuming an awk with ENVIRON):
awk '$1 in ENVIRON {print $1"="ENVIRON[$1]}' varfile
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'
}