Slow process in code - awk

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

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

Pad a file using awk

So I have this working on a server with awk 3.1.7 and when I try using on a server with awk 4.0.2 it does not seem to work. I am just trying to add a filename 14 digits, is the goal. I don't have the perl version on rename just in case it is brought up.
Anyhow this is my code
ls 800001.1.pull | awk -F'.pull' '{ printf "%s %014s.pull%s\n", $0, $1, $2; }' | xargs -n 2 mv
I get this error mv: ‘800001.1.pull’ and ‘800001.1.pull’ are the same file
ls 800001.1.pull | awk -F'.pull' '{ printf "%s %014s.pull%s\n", $0, $1, $2; }' | xargs -n 2 mv
mv: ‘800001.1.pull’ and ‘800001.1.pull’ are the same file
This same things that has been running on the other server for a few years and I don't get an error, I get a file named:
ls 800001.1.pull | awk -F'.pull' '{ printf "%s %014s.pull%s\n", $0, $1, $2; }' | xargs -n 2 mv
000000800001.1.pull
and this is why I said this:
I will take it that the newer awk handles delimiters different?
The expected output is 000000800001.1.pull
The original file needs to be 0 padded to 14.
Thanks!
$ echo 800001.1.pull | awk -F'.pull' '{ new=sprintf("%14s%s",$1,FS); gsub(/ /,0,new); print $0, new }'
800001.1.pull 000000800001.1.pull
$ echo 800001.12.345.pull | awk -F'.pull' '{ new=sprintf("%14s%s",$1,FS); gsub(/ /,0,new); print $0, new }'
800001.12.345.pull 0800001.12.345.pull

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

Convert a decimal data field to hexadecimal using sed or awk

Who can correct this command to get the desired output :
input : "1|2|30|4"
echo "1|2|30|4" | awk -F, -v OFS=| '{print $1,$2; printf "%04X", $3; print $4}'
Output expected :
1|2|001E|4
Best regards.
$ echo "1|2|30|4" |
awk -F'|' -v OFS='|' '{print $1, $2, sprintf("%X", $3), $4}'
1|2|1E|4
echo "1|2|30|4" | awk -F"|" '{printf "%s|%s|%04X|%s", $1, $2, $3, $4}'
Output:
1|2|001E|4

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.