Awk last field string substitution - awk

I am trying to get the last filed using string substiution of following output using awk -
ps -ef |grep -i "[o]cssd.bin"
Output:
grid 47275 1 1 Sep23 ? 17:49:39 /opt/grid/12.1/bin/ocssd.bin
used awk as -
$ ps -ef | grep -i "[o]cssd.bin" | awk '{ gsub("/ocssd.bin",""); print $NF}'
output:
$NF}
/opt/grid/12.1/bin
How to avoid "$NF}" ? I only need "/opt/grid/12.1/bin" ..!

try:
ps -ef | grep -i "[o]cssd.bin" | awk '{ if(gsub("/ocssd.bin","")) print $NF}'

Related

AWK Filter and then trim output

I am looking to trim the output below
curl -s -L https://www.citrix.com/downloads/workspace-app/mac/workspace-app-for-mac-latest.html#ctx-dl-eula-external | awk '/<p>Version: / {print $1}'
Current Output: <p>Version: 20.08.0.3
Desired Output: 20.08.0.3
Could you please try following, written and tested with shown samples only.
your_command | awk '
match($0,/<p>Version: ([0-9]+\.){3}[0-9]+/){
val=substr($0,RSTART,RLENGTH)
sub(/.*;/,"",val)
print val
val=""
}'
curl -s -L https://www.citrix.com/downloads/workspace-app/mac/workspace-app-for-mac-latest.html#ctx-dl-eula-external | awk '{print substr($1,index($1,";")+1)}'

Z Shell: Inputing Alias Content Produces Different Output Than Calling Alias Itself

I type this command:
$ sudo cat /etc/wireguard/nl1.conf | grep PrivateKey | awk '{ print $3 }' | xargs printf "\033[1;34m%s\033[0m\n"
PqyfQ3CtdcoCwgQjW8iGbypofi4TUyJSS5PmVa67sPCTS=
The output is bold, as expected.
When calling this alias (set in .zshrc):
alias mlprivkey="sudo cat /etc/wireguard/nl1.conf | grep PrivateKey | awk '{ print $3 }' | xargs printf \"\033[1;34m%s\033[0m\n\""
… the first two lines of the following output is not expected:
$ mlprivkey
PrivateKey
=
PqyfQ3CtdcoCwgQjW8iGbypofi4TUyJSS5PmVa67sPCTS=
I thought the alias would output same result as typing the command set itself.

How to grep the outputs of awk, line by line?

Let's say I have the following text file:
$ cat file1.txt outputs
MarkerName Allele1 Allele2 Freq1 FreqSE P-value Chr Pos
rs2326918 a g 0.8510 0.0001 0.5255 6 130881784
rs2439906 c g 0.0316 0.0039 0.8997 10 6870306
rs10760160 a c 0.5289 0.0191 0.8107 9 123043147
rs977590 a g 0.9354 0.0023 0.8757 7 34415290
rs17278013 t g 0.7498 0.0067 0.3595 14 24783304
rs7852050 a g 0.8814 0.0006 0.7671 9 9151167
rs7323548 a g 0.0432 0.0032 0.4555 13 112320879
rs12364336 a g 0.8720 0.0015 0.4542 11 99515186
rs12562373 a g 0.7548 0.0020 0.6151 1 164634379
Here is an awk command which prints MarkerName if Pos >= 11000000
$ awk '{ if($8 >= 11000000) { print $1 }}' file1.txt
This command outputs the following:
MarkerName
rs2326918
rs10760160
rs977590
rs17278013
rs7323548
rs12364336
rs12562373
Question: I would like to feed this into a grep statement to parse another text file, textfile2.txt. Somehow, one pipes the output from the previous awk command into grep AWKOUTPUT textfile2.txt
I would like each row of the awk command above to be grepped against textfile2.txt, i.e.
grep "rs2326918" textfile2.txt
## and then
grep "rs10760160" textfile2.txt
### and then
...
Naturally, I would save all resulting rows from textfile2.txt into a final file, i.e.
$ awk '{ if($8 >= 11000000) { print $1 }}' file1.txt | grep PIPE_OUTPUT_BY_ROW textfile2.txt > final.txt
How does one grep from a pipe line by line?
EDIT: To clarify, the one constraint I have is that file1.txt is actually the output of a previous pipe. (I'm trying to simplify the question somewhat.) How would that change the answer?
awk + grep solution:
grep -f <(awk '$8 >= 11000000{ print $1 }' file1.txt) textfile2.txt > final.txt
-f file - obtain patterns from file, one per line
You can use bash to do this:
bash-3.1$ echo "rs2326918" > filename2.txt
bash-3.1$ (for i in `awk '{ if($8 >= 11000000) { print $1 }}' file1.txt |
grep -v MarkerName`; do grep $i filename2.txt; done) > final.txt
bash-3.1$ cat final.txt
rs2326918
Alternatively,
bash-3.1$ cat file1.txt | (for i in `awk '{ if($8 >= 11000000) { print $1 }}' |
grep -v MarkerName`; do grep $i filename2.txt; done) > final.txt
The switch grep -v tells grep to reverse its usual activity and print all lines that do not match the pattern. This switch "inVerts" the match.
only using awk can do this for you:
$ awk 'NR>1 && NR==FNR {if ($8 >= 110000000) a[$1]++;next} \
{ for(i in a){if($0~i) print}}' file1.txt file2.txt> final.txt

printf usage with awk when printing multiple columns

I am trying this below command:
cat dcl1serrfip_check.csv | grep -Fi 'BANK0_F5_WRDAT_P0[0]' | grep -i setup | grep 'L2H' | grep highv | grep -i low | awk -F ',' -v dev="0.861" -v rc="1.105" -v inte="0.872" '{ print ($10+$11)-(($12+$13)-($14))","($10*dev)+($11*rc)-(($12*dev)+($13*rc)-($14*inte))}'
This gives below output:
-6.93889e-18,0.000288
I want this output to be formatted to 4 decimal places. How to do it? The desired output would be
-0.0000,0.0002
You need, %0.4f or %.4f
To Test use :
awk 'BEGIN{ printf("%0.4f\n", -6.93889e-18) }'
So it becomes:
printf("%0.4f,%0.4f\n", ($10+$11)-(($12+$13)-($14)), ($10*dev)+($11*rc)-(($12*dev)+($13*rc)-($14*inte)) )
Actually you can rewrite your command in awk itself, no need of so many grep and cat combination

while read line in awk and using $line

this is my file
air1.txt
fc:75:16:d2:91:a3 -90 targol
78:54:2e:7f:e8:9e -88 DLink
fc:75:16:d2:91:a3 -89 targol
78:54:2e:7f:e8:9e -89 DLink
78:54:2e:7f:e8:9e -88 DLink
78:54:2e:7f:e8:9e -87 DLink
fc:75:16:d2:91:a3 -90 targol
I want to calculate the average of second column for each name in the third column! Here is my scrip!
RSSI=$(awk '{print $3}' air1.txt | sort -u | while read line; do awk < air1.txt '{print $2,$3}' | grep $line | ./rssiMean.sh |cut -d'.' -f1 |awk '{print $line,$1}' ;done)
echo $RSSI
but the result is
-88 -88 -89 -89
Why I can't get $line?!
BTW ./rssiMean.sh calculate the average!
This should do:
awk '{a[$3]+=$2;b[$3]++} END {for (i in a) print i,a[i]/b[i]}' air1.txt
DLink -88
targol -89.6667
It sum up number for every data in column #3 and divide it by number of hits.
You cannot use bash variables as such in awk script.
Rather use a awk variable and assign the value using the -v paramter
eg:
$var=123
$awk -v awkvar=$var '{print awkvar}'
here awkvar is an awk varible created and passed with value of $var
to make this change in your script
RSSI=$(awk '{print $3}' air1.txt | sort -u | while read line; do awk < air1.txt '{print $2,$3}' | grep $line | ./rssiMean.sh |cut -d'.' -f1 |awk -v line=$line '{print line,$1}' ;done)
echo $RSSI
The change made is
awk -v line=$line '{print line,$1}
awk varible line is assigned with value of bash varibale $line