Using a variable as a operand in a expression - awk - 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.

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

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

Need to retrieve a value from an HL7 file using awk

In a Linux script program, I've got the following awk command for other purposes and to rename the file.
cat $edifile | awk -F\| '
{ OFS = "|"
print $0
} ' | tr -d "\012" > $newname.hl7
While this is happening, I'd like to grab the 5th field of the MSH segment and save it for later use in the script. Is this possible?
If no, how could I do it later or earlier on?
Example of the segment.
MSH|^~\&|business1|business2|/u/tmp/TR0049-GE-1.b64|routing|201811302126||ORU^R01|20181130212105810|D|2.3
What I want to do is retrieve the path and file name in MSH 5 and concatenate it to the end of the new file.
I've used this to capture the data but no luck. If fpth is getting set, there is no evidence of it and I don't have the right syntax for an echo within the awk phrase.
cat $edifile | awk -F\| '
{ OFS = "|"
{fpth=$(5)}
print $0
} ' | tr -d "\012" > $newname.hl7
any suggestions?
Thank you!
Try
filename=`awk -F'|' '{print $5}' $edifile | head -1`
You can skip the piping through head if the file is a single line
First of all, it must be mentioned that the awk line in your first piece of code, has zero use:
$ cat $edifile | awk -F\| ' { OFS = "|"; print $0 }' | tr -d "\012" > $newname.hl7
This is totally equivalent to
$ cat $edifile | tr -d "\012" > $newname.hl7
because OFS is only used to redefine $0 if you redefine a field.
Example:
$ echo "a|b|c" | awk -F\| '{OFS="/"; print $0}'
a|b|c
$ echo "a|b|c" | awk -F\| '{OFS="/"; $1=$1; print $0}'
a/b/c
I understand that you have a hl7 file in which you have a single line starting with the string "MSH". From this line you want to store the 5th field: this is achieved in the following way:
fpth=$(awk -v outputfile="${newname}.hl7" '
BEGIN{FS="|"; ORS="" }
($1 == "MSH"){ print $5 }
{ print $0 > outputfile }' $edifile)
I have replaced ORS to an empty character set, as it is equivalent to tr -d "\012". The above will work very nicely if you only have a single MSH in your file.

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