Let's say I have a file.csv
as the following:
id;filename;device
1;118574934-20220503-17h44m20s101;
2;118574934-20220503-17h44m20s101;
3;118574934-20220503-17h44m20s101;DEVICE-0001-33
4;118574934-20220503-17h44m20s101;DEVICE-0001-33
5;118574934-20220503-17h44m20s101;DEVICE-0001-33
6;118574934-20220503-17h44m20s101;DEVICE-0001-33
7;118574934-20220503-17h44m20s101;DEVICE-0001-33
I've created an awk
script for handling getting the device name when it appears on my file and then it finishes its execution. The script.awk
is functional and is as the following:
BEGIN {
FS=";"
}
NR > 1 { fileName = $2 }
NR > 1 { if ( $3 != "" ) { device = $3; exit} }
END {
if ( device == "" ) {
line = "UNCONNECTED_"fileName".txt;UNCONNECTED"
} else {
line = device"_"fileName".txt;"device
}
print "filename;folder"
print line
}
Its output after executing awk -f script.awk file.csv
is:
filename;folder
DEVICE-0001-33_118574934-20220503-17h44m20s101.txt;DEVICE-0001-33
The problem is that I'd like to split the string DEVICE-0001-33
into DEVICE-0001
. Since I've already used FS
with ;
and I can't use FS
again as -
in order to split my device
variable. How can I handle that using only awk
? How can I split a variable with a specific character once FS
is already being used?