I've been fighting with awk to make this work but I've been unable to do it. I have two lines as follows:
= filename: /path/to/file
years="1990,2001"
I need to check each year between the quotes against a given value and then print the previous line if it matches and get the filename as a result (it only needs to match the first one found). The value and operator i.e. <,>,=,<=,=>,~
will be passed via a variable to awk like:
value="2000"
string"=\$2 < $value" # just an example
awk ... '"$string"' ...
There are conditional statements which can generate this string based on the input received.
I've tried separating each field using a space, quote and comma as a delim:
awk -F'[," ]' '{i=(1+(i%N));if(buf[i]&& $2<2000) print buf[i]; buf[i]=$0;}'
This works but I need to loop through all the columns += $2. I tried to do something like this:
awk -F'[," ]' '{for(f=2;f<=NF;f++);i=(1+(i%N));if(buf[i]&& $f>1950) print buf[i]; buf[i]=$0;}'
But that didn't work (I'm probably just doing it wrong).
I also considered getting rid of if(buf[i]&& $2>1950) print buf[i]; buf[i]=$0;
and just joining the two lines and separating the fields, checking with the loop on += $5 and then printing just "$3" since that will always be the filename, but I can't figure out how to merge the two lines into one.
Example:
year < 2000
Input text:
= filename: /mnt/project1/record1.txt
years="2005,2019,2011,2012,2013"
= filename: /mnt/project1/record2.txt
years="1996,2000"
= filename: /mnt/project1/record3.txt
years="2005,2001,2012"
= filename: /mnt/project1/record4.txt
years="2010,2009,1997,2000"
Output (match):
/mnt/project1/record2.txt
/mnt/project1/record4.txt