Hello all,
I have since given up trying to figure this out and used sed instead, but I am trying to understand awk and was wondering how someone might do this in awk.
I am trying to match on the first field of a specific file with the first field on multiple files, and append the second field of the first file on multiple files (preferably inplace).
Example:
file1
1000/3333,20150101-96
1000/4444,20150102-02
1000/5555,20150103-29
1000/6666,20150104-67
file2
1000/3333
9999/9999
file3
1000/6666
8888/8888
Preferred output:
file2
1000/3333,20150101-96
9999/9999
file3
1000/6666,20150104-67
8888/8888
I have seen plenty of examples on how to work with two files, or storing one field in an array and only acting on that one field.
I stole the code below and modified it for my purpose, but I still don't understand how I could perform this on multiple files or if it's possible to edit in-place (gawk -i inplace):
#!/bin/bash
INPUTFILE="/home/username/blah/file1"
DATAFILE="/home/username/blah/file2"
OUTFILE="/home/username/pleasework.out"
awk 'BEGIN {
while (getline < "'"$INPUTFILE"'")
{
split($0,a,",");
name=a[1];
date=a[2];
key=date
data=name
nameofarray[data]=key;
}
close("'"$INPUTFILE"'");
while (getline < "'"$DATAFILE"'")
{
var=nameofarray[$0];
print $0","var > "'"$OUTFILE"'";
}
}'