AWK Filter and then trim output - awk

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

Related

AWK parsing top command output

AWK code:
top -b|head -20|awk '/PID/,EOF {print $0}'|grep -v PID|while read line; do awk -v MYHOST=$(hostname) '{print "topstat,host="MYHOST",PID="$1" USER="$2",PR="$3",NI="$4",VIRT="$5",RES="$6",SHR="$7",STATE="$8",%CPU="$9",%MEM="$10",TIME="$11,COMMAND="$12}';done
The o/p line(1 line for e.g) is
topstat,host=host1.abc.com,PID=14 USER=root,PR=rt,NI=0,VIRT=0,RES=0,SHR=0,STATE=S,%CPU=0.0,%MEM=0.0,TIME=0:00.36,COMMAND=migration/1
I need the O/p
topstat,host=host1.abc.com,PID=14 USER=root,PR=rt,NI=0,VIRT=0,RES=0,SHR=0,STATE="S",%CPU=0.0,%MEM=0.0,TIME=0:00.36,COMMAND="migration/1"
Any help?
You need to use \ escape character
top -b|head -20|tail -n +6 | awk -v MYHOST=$(hostname) '{print "topstat,host="MYHOST",PID="$1" USER="$2",PR="$3",NI="$4",VIRT="$5",RES="$6",SHR="$7",STATE=\""$8"\",%CPU="$9",%MEM="$10",TIME="$11,COMMAND="$12}'

awk split with asterix

I am trying to split a variable as follows. is there any efficient way to do this preferably using awk.
echo 262146*10,69636*32 |awk -F, 'split($1, DCAP,"\\*") {print DCAP[1]}; split($2, DCAP,"\\*"){print DCAP[1]}'
echo '262146*10,69636*32' | awk -F '[,*]' '{print $1; print $3}'
or
echo '262146*10,69636*32' | awk -F '[,*]' '{printf("%d\n%d\n",$1,$3)}'
Output:
262146
69636
If you have a longer sequence you could try:
echo 262146*10,69636*32,10*3 | awk 'BEGIN {FS="*"; RS=","} {print $1}'

Slow process in code

can you help to improve this code to be faster .. with 50000 lines in my file this take a lot time.
I appreciate your help
input
17/11/27 03:13:50:480000
17/11/27 03:12:54:380000
17/11/27 03:14:39:980000
output
1195787648480000
1195787592380000
1195787697980000
my code
ts=$(date -d'01/06/1980 00:00:00' +%s)
lap=18
cat file |
while read tt
do
dt=`echo $tt | awk '{print $1}' | awk -F"/" '{print $2"/"$3"/"$1}'`
tm=`echo $tt | awk '{print substr($2,1,8)}'`
ms=`echo $tt | awk '{print $2}' | awk -F":" '{print $NF}'`
line=`echo $dt" " $tm`
echo $line\ $(date -d "${line/// }" "+%s") |
awk '{print (($3 - '$ts') + '$lap')'$ms'}'
done
Please, help me to improve my code to get results faster.
Many thanks.
With single GNU awk process:
awk -F'[[:space:]]*|/|:' -v ts=$(date -d'01/06/1980 00:00:00' +%s) -v lap=18 '{
print (mktime(sprintf("20%d %d %d %d %d %d",$1,$2,$3,$4,$5,$6)) - ts)+lap $NF
}' file
The output:
1195791248480000
1195791192380000
1195791297980000
Enjoy )
similar with gawk
$ awk -F'[/: ]' -v ts=$(date -d'01/06/1980' +%s) \
-v lap=18 '{ms=$NF; $NF=""; d=sprintf(20$0);
print mktime(d)+lap-ts ms}' file
1195787648480000
1195787592380000
1195787697980000

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

Using a variable as a operand in a expression - awk

I tried doing something like this in awk?
limit=10000
ls -ltr | awk '$5 >= $limit { print $5 }'
But it doesn't seem to work, it prints all size less than the limit too.
Thanks in advance!
Try to set variable in AWK's paramaters using -v option:
limit=10000
ls -ltr | awk -v mylimit=$limit '$5 >= mylimit {print $5}'
Change your awk command like below,
ls -ltr | awk -v limit=10000 '$5 >= limit { print $5 }'
you could declare an variable in awk itself using -v switch.