Timeline for Check numbers in each line on a specific column in one variable against all lines in two specific columns in another variable using awk
Current License: CC BY-SA 4.0
9 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| May 12, 2022 at 8:11 | comment | added | AdminBee | @Giles The problem may now be sufficiently distinct to warrant a new, follow-up question (you can include a link to this question to establish context). | |
| May 11, 2022 at 16:51 | comment | added | Giles | @AdminBee Sorry I should also mention that I adapted to handle an additional column in the variable list to check against, that bit did work. I would really appreciate it if you can explain where I'm going wrong | |
| May 11, 2022 at 16:47 | comment | added | Giles | I've gone back through the output and it would be even better if I could add column 1 into the command, where column one in both variables have to be identical. I tried adapting the code here for that and failed. I'm clearly misunderstanding something. Do I need to make a new question, or edit this question. I tried: awk -F',' 'list=="constraints"{n++; low[n]=$2;high[n]=$3;c[n]=$1;next} {for (i=1;i<=n;i++) {if (($2>=low[i]&&$2<=high[i])||($3>=low[i]&&$3<=high[i])&&($1==c[i])) {print;next};}}' list=constraints <(echo "$ListToCheckAgainst") list=check <(echo "$ListToCheckFrom") | |
| May 11, 2022 at 16:09 | comment | added | Ed Morton |
I understand where you're coming from and you're right it's not a BAD idea but personally I'd only consider using a flag if/when I need it, which is very rarely, and even then I usually wouldn't use one. If I needed to care about a first file being empty and it's a case where I'm not worried about portability then I'd use ARGIND==1 since I use gawk, and if I am then I'd usually use FILENAME==ARGV[1]. But using a flag is fine too.
|
|
| May 11, 2022 at 15:41 | comment | added | AdminBee |
@EdMorton You are right in that in this case, there would effectively be no difference. But in general it is more robust, because while with the variable-setting "in between files" you simply would read no constraints (which is reasonable if there aren't any for a particular run), using the NR==FNR test with an empty first file would make your program mistake the second file for the first, which has repercussions if the goal is not simply to filter lines. So I think the habit of using these temporary variables does have its merits.
|
|
| May 11, 2022 at 15:34 | comment | added | Ed Morton |
@AdminBee if the first file was empty and the script used NR==FNR it wouldn't fail, they'd just get no output same as if they were setting flags between files.
|
|
| May 11, 2022 at 15:07 | vote | accept | Giles | ||
| May 11, 2022 at 14:56 | history | edited | AdminBee | CC BY-SA 4.0 |
Typos
|
| May 11, 2022 at 14:44 | history | answered | AdminBee | CC BY-SA 4.0 |