$1 in a line of file 1
matches
substr($5,1,5) in a line of file 2
I want to add $2 of file 1 to the end of the relevant line in file 2.
I am stuck, if you will, help please.
Explanation of superflous code,
1) there are multiple versions of "summary_*.csv". The file names include a date and time stamp of when the files were created.
I want to scan only the most recently created file and I thought the
"if (file_number == 2)"
would restrict the script to the most recent file but it didn't.
For reasons unknown the script found an older file first so I have added the full date and time stamp to name of "summary_*.csv" in the std in.
Question, if it is possible, how would you suggest I restrict the script to scanning the most recent version of summary_*.csv .... so that I do not have have to update this script when a new version of summary_* is produced?
2) "if (FNR < 7) {print $0}" .prints column titles.
3) the " print number[$1] > ("LG_test.csv")" is a test to see whether or not the array number[$1] element contains the contents of $2, it appears that it does at that point in the script.
Currently the output file "summary_location_test.csv" contains only the column titles.
I repaced
if ( ( number[substr($5,1,5)] !=0 ) && ( number[substr($5,1,5)] !="") ) {print $0 ", test ," substr($5,1,5) "," number[substr($5,1,5)] }
with
if (subtr($5,1,5) in number) { print $0 ", test ," substr($5,1,5) "," number[substr($5,1,5)] }
but it didn't work.
Code: Select all
gawk -F[,"\t"] '{
if (NR == 1) {file_number =1 }
if (file_number == 1) {number[$1] = $2
print number[$1] > ("LG_test.csv")
}
if ( (FNR ==1) && (NR >=2) ) {file_number++ }
if ((file_number == 2)) {
if (FNR <7) {print $0 }
if (FNR >=7) {
if ( ( number[substr($5,1,5)] !=0 ) && ( number[substr($5,1,5)] !="") ) {
print $0 ", test ," substr($5,1,5) "," number[substr($5,1,5)]
}
}
}
}' URL_list_temp.txt summary_2024-03-01_[09-10-11].csv > summary_location_test.csv