I have working code
BEGIN { FS=";"; } # field separator
{
if (match($2, /[0-9]+/)) { # matching `ID` value
m=substr($2, RSTART, RLENGTH);
a[m]++; # accumulating number of lines for each `ID`
print > m"_count.txt"; # writing lines pertaining to certain `ID` into respective file
}
}
END {
for(i in a) {
print "mv "i"_count.txt "i"_"a[i]".txt" # renaming files with actual counts
}
}
Now i need to change it to do something like this. So i have three arrays of IDs and each array means separate folders to save result.
BEGIN { FS=";"; } # field separator
{
array1=(125 258 698 874)
array2=(956 887 4455 22)
array3=(111 444 558 966 332)
if ($1 == $2) {varR=$3} else {varR=$2}
if (match(varR, /[0-9]+/)) { # matching `ID` value
if ( varR in array1 ) {
FolderName = "folder1/"
m1=substr(varR, RSTART, RLENGTH);
a1[m1]++; # accumulating number of lines for each `ID`
print > (FolderName m1)"_count.txt"; # writing lines pertaining to certain `ID` into respective file
}
if ( varR in array2 ) {
FolderName = "folder2/"
m2=substr(varR, RSTART, RLENGTH);
a2[m2]++; # accumulating number of lines for each `ID`
print > (FolderName m2)"_count.txt"; # writing lines pertaining to certain `ID` into respective file
}
if ( varR in array3 ) {
FolderName = "folder3/"
m3=substr(varR, RSTART, RLENGTH);
a3[m3]++; # accumulating number of lines for each `ID`
print > (FolderName m3)"_count.txt"; # writing lines pertaining to certain `ID` into respective file
}
}
}
END {
for(i in a1) {
print "mv "i"_count.txt "i"_"a1[i]".txt" # renaming files with actual counts
}
for(i in a2) {
print "mv "i"_count.txt "i"_"a2[i]".txt" # renaming files with actual counts
}
for(i in a3) {
print "mv "i"_count.txt "i"_"a3[i]".txt" # renaming files with actual counts
}
}
As i need to save matching ID to txt files and put in needed folder What if i have 100 arrays? I need to duplicate code for each one?