printf usage with awk when printing multiple columns - awk

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

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.

match duplicate string before a specified delimiter

cat test.txt
serverabc.test.net
serverabc.qa.net
serverabc01.test.net
serverstag.staging.net
serverstag.test.net
here i need to match the duplicate strings just before the delimiter '.'
So the expected output would be like below. because string "serverabc" and "serverstag" found to be duplicates. Please help.
serverabc.test.net
serverabc.qa.net
serverstag.staging.net
serverstag.test.net
awk to the rescue!
$ awk -F\. '{c[$1]++; a[$1]=a[$1]?a[$1]RS$0:$0}
END{for(k in c) if(c[k]>1) print a[k]}' file
serverabc.test.net
serverabc.qa.net
serverstag.staging.net
serverstag.test.net
If it is not going to be used allot I would probably just do something like this:
cut -f1 -d\. foo.txt | sort |uniq -c | grep -v " 1 " | cut -c 9-|sed 's/\(.*\)/^\1\\./' > dup.host
grep -f dup.host foo.txt
serverabc.test.net
serverabc.qa.net
serverstag.staging.net
serverstag.test.net

Awk last field string substitution

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}'

sum occurrence output of uniq -c

I want to sum up occurrence output of "uniq -c" command.
How can I do that on the command line?
For example if I get the following in output, I would need 250.
45 a4
55 a3
1 a1
149 a5
awk '{sum+=$1} END{ print sum}'
This should do the trick:
awk '{s+=$1} END {print s}' file
Or just pipe it into awk with
uniq -c whatever | awk '{s+=$1} END {print s}'
for each line add the value of of first column to SUM, then print out the value of SUM
awk is a better choice
uniq -c somefile | awk '{SUM+=$1}END{print SUM}'
but you can also implement the logic using bash
uniq -c somefile | while read num other
do
let SUM+=num;
done
echo $SUM
uniq -c is slow compared to awk. like REALLY slow.
{mawk/mawk2/gawk} 'BEGIN { OFS = "\t" } { freqL[$1]++; } END { # modify FS for that
# column you want
for (x in freqL) { printf("%8s %s\n", freqL[x], x) } }' # to uniq -c upon
if your input isn't large like 100MB+, then gawk suffices after adding in the
PROCINFO["sorted_in"] = "#ind_num_asc"; # gawk specific, just use gawk -b mode
if it's really large, it's far faster to use mawk2 then pipe to to
{ mawk/mawk2 stuff... } | gnusort -t'\t' -k 2,2
While the aforementioned answer uniq -c example-file | awk '{SUM+=$1}END{print SUM}' would theoretically work to sum the left column output of uniq -c so should wc -l somefile as mentioned in the comment.
If what you are looking for is the number of uniq lines in your file, then you can use this command:
sort -h example-file | uniq | wc -l