Sunday, July 12, 2020

Catatan AWK

#cat a
1 = a
2 = b
3 = c
4 = d
5 = e
6 = f
7 = g
8 = h
9 = i

#cat b
a - one
c - three
e - five
g - seven
i - nine


#awk '{print $0,"FNR"FNR,"NR="NR}' a b
1 = a FNR1 NR=1
2 = b FNR2 NR=2
3 = c FNR3 NR=3
4 = d FNR4 NR=4
5 = e FNR5 NR=5
6 = f FNR6 NR=6
7 = g FNR7 NR=7
8 = h FNR8 NR=8
9 = i FNR9 NR=9
a - one FNR1 NR=10
c - three FNR2 NR=11
e - five FNR3 NR=12
g - seven FNR4 NR=13
i - nine FNR5 NR=14

#awk '{print $0,"FNR"FNR,"NR="NR}' b a
a - one FNR1 NR=1
c - three FNR2 NR=2
e - five FNR3 NR=3
g - seven FNR4 NR=4
i - nine FNR5 NR=5
1 = a FNR1 NR=6
2 = b FNR2 NR=7
3 = c FNR3 NR=8
4 = d FNR4 NR=9
5 = e FNR5 NR=10
6 = f FNR6 NR=11
7 = g FNR7 NR=12
8 = h FNR8 NR=13
9 = i FNR9 NR=14

FNR=number of row of the current file which have been read
 NR=total number of rows which have been read

FNR==NR will get the first input file
#awk 'FNR==NR {print $0,"FNR"FNR,"NR="NR}' a b
1 = a FNR1 NR=1
2 = b FNR2 NR=2
3 = c FNR3 NR=3
4 = d FNR4 NR=4
5 = e FNR5 NR=5
6 = f FNR6 NR=6
7 = g FNR7 NR=7
8 = h FNR8 NR=8
9 = i FNR9 NR=9
#awk 'FNR==NR {print $0,"FNR="FNR,"NR="NR}' b a
a - one FNR=1 NR=1
c - three FNR=2 NR=2
e - five FNR=3 NR=3
g - seven FNR=4 NR=4
i - nine FNR=5 NR=5

FNR<NR will get the second input file
#awk 'FNR<NR {print $0,"FNR="FNR,"NR="NR}' a b
a - one FNR=1 NR=10
c - three FNR=2 NR=11
e - five FNR=3 NR=12
g - seven FNR=4 NR=13
i - nine FNR=5 NR=14
#awk 'FNR<NR {print $0,"FNR"FNR,"NR="NR}' b a
1 = a FNR1 NR=6
2 = b FNR2 NR=7
3 = c FNR3 NR=8
4 = d FNR4 NR=9
5 = e FNR5 NR=10
6 = f FNR6 NR=11
7 = g FNR7 NR=12
8 = h FNR8 NR=13
9 = i FNR9 NR=14


#awk 'FNR==NR {x[$1]=$1;print x[$1]}' a b
1
2
3
4
5
6
7
8
9
#awk 'FNR==NR {x[$1]=$2;print x[$1]}' a b
=
=
=
=
=
=
=
=
=
#awk 'FNR==NR {x[$1]=$3;print x[$1]}' a b
a
b
c
d
e
f
g
h
i
#awk 'FNR<NR {x[$1]=$1;print x[$1]}' a b
a
c
e
g
i
#awk 'FNR<NR {x[$1]=$2;print x[$1]}' a b
-
-
-
-
-
#awk 'FNR<NR {x[$1]=$3;print x[$1]}' a b
one
three
five
seven
nine


{x[$3];next} stores in x[]  the third column of the first file (FNR==NR) and goes to the next line.
$1 in x is evaluated column 1 of second file when looping. It checks if the current line is within the x[] array.
#awk 'FNR==NR{x[$3];next} $1 in x' a b
a - one
c - three
e - five
g - seven
i - nine

{x[$1];next} stores in x[]  the first column of the first file (FNR==NR) and goes to the next line.
$3 in x is evaluated column 3 of second file when looping. It checks if the current line is within the x[] array.
#awk 'FNR==NR{x[$1];next} $3 in x' b a
1 = a
3 = c
5 = e
7 = g
9 = i


Reads file b, storing all into an string-indexed array (eg x["a"] == "one" ; x["c"] == "three" )
if X[?] not blank, replace column 3 of second file with the x[?] value
while read second file (FNR<NR) print it

REPLACE
#awk 'FNR==NR{x[$1]=$3}(x[$3]!=""){$3=x[$3]} FNR<NR { print}' b a
1 = one
2 = b
3 = three
4 = d
5 = five
6 = f
7 = seven
8 = h
9 = nine

MERGE
#awk 'FNR==NR{x[$1]=$0} FNR<NR {print $0,x[$3]}' b a
1 = a a - one
2 = b
3 = c c - three
4 = d
5 = e e - five
6 = f
7 = g g - seven
8 = h
9 = i i - nine

Mengambil Tanggal dan Waktu pada batch file.


OPSI 1:
@echo off

::Previous Windows %DATE% was "MM/DD/YYYY" -not- "DAY MM/DD/YYYY", so get it from Right to left first.
set WJ_date=%date:~-10%
set WJ_date=%WJ_date:~6,4%%WJ_date:~0,2%%WJ_date:~3,2%
echo WJ_date=%WJ_date%

:: Replace ' ' with '0' before 10am
set WJ_time=%TIME: =0%
set WJ_time=%WJ_time:~0,2%%WJ_time:~3,2%%WJ_time:~6,2%
echo WJ_time=%WJ_time%

echo.
pause

OPSI 2:
@echo off

::Previous Windows %DATE% was "MM/DD/YYYY" -not- "DAY MM/DD/YYYY" !
::for /f "tokens=1,2,3,4 delims=/ " %%a in ("%DATE%")  do set MM=%%b&set DD=%%c&set YYYY=%%d
for   /f "tokens=1,2,3,4 delims=/ " %%a in ('date /t') do set MM=%%b&set DD=%%c&set YYYY=%%d
set  WJ_date=%YYYY%%MM%%DD%
echo WJ_date=%WJ_date%

for /f "tokens=1,2,3,4 delims=:." %%a in ("%TIME: =0%")  do set hh=%%a&set nn=%%b&set ss=%%c
set WJ_time=%hh%%nn%%ss%
echo WJ_time=%WJ_time%

echo.
pause


Reff: https://www.dostips.com/DtTipsStringManipulation.php