Timeline for Pass shell variable as a /pattern/ to awk
Current License: CC BY-SA 4.0
11 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Aug 26, 2022 at 13:21 | history | edited | Stéphane Chazelas | CC BY-SA 4.0 |
missing -e (or --). Can't use `echo` for arbitrary data. awk can stop at the first match as well.
|
| Mar 21, 2016 at 10:14 | comment | added | glenn jackman | Yes that's right. | |
| Mar 21, 2016 at 7:05 | comment | added | XXX |
It seems that this: awk '$0 ~ /foo/ { print $0 }' is actually equivalent to this: awk -v pattern=foo '$0 ~ pattern { print $0 }. In other words, the // brackets are not needed anymore at all, because pattern becomes a dynamic regexp. Is that right?
|
|
| Mar 21, 2014 at 16:15 | comment | added | glenn jackman |
Don't write /$0 ~ search/ -- leave out the slashes: $0 ~ search
|
|
| Mar 21, 2014 at 15:57 | comment | added | glenn jackman |
Please show the contents of $line -- do it in your question for proper formatting.
|
|
| Mar 21, 2014 at 15:53 | comment | added | branquito |
maybe it has something to do with the way $line is retrieved, pattern search is done on the output of whois $line, $line coming from file in a WHILE DO block.
|
|
| Mar 21, 2014 at 15:42 | comment | added | branquito |
yes I noticed, it needs to be set on BEGIN block to zero each time, as it serves as a switch. But interestingly I tried now script using $0 ~ pattern, and it does not work, however with /'"$1"'/ it does work!? :O
|
|
| Mar 21, 2014 at 15:35 | comment | added | glenn jackman |
I didn't note the removal of the BEGIN block: an unassigned variable is treated as 0 in a numeric context or the empty string otherwise. So, an unassigned variable will be false in if (p) ...
|
|
| Mar 21, 2014 at 15:30 | vote | accept | branquito | ||
| Mar 21, 2014 at 15:30 | comment | added | branquito | This is exactly what solves this in a way I wanted (1st example), because it keeps the semantics, which was my goal. Thanks. | |
| Mar 21, 2014 at 15:16 | history | answered | glenn jackman | CC BY-SA 3.0 |