Skip to main content
typo fix
Source Link
αғsнιη
  • 41.9k
  • 17
  • 75
  • 117

If you want the pattern to match exactly on either "." (with quotes included), 0 or 1, you'd need the pattern to be ^("\."|[01])$ or ^("[.]"|[01])$ or ^("\."|0|1)$, etc.

But when using -v to pass that pattern to awk, you have the problem that awk treats \ specially there (same happens for -F x which is similar to -v FS=x), so you'd need to escape the backslashes here.

It's better to use ENVIRON to pass arbitrary strings from shell to awk as that doesn't have that issue.

So:

pattern='"\."|0|1'
PATTERN=$pattern DELIMITER=$delimiter awk -nv n="$n" '
  BEGIN {FS = ENVIRON["DELIMITER"]; m = ENVIRON["PATTERN"]}
  $n ~ "^(" m ")$" {...}'

(still using -v for n as those are expected to be numbers so without backslash).

Note the (, ) above. ^x|y$ would be either x at the start or y at the end.

If you want the pattern to match exactly on either "." (with quotes included), 0 or 1, you'd need the pattern to be ^("\."|[01])$ or ^("[.]"|[01])$ or ^("\."|0|1)$, etc.

But when using -v to pass that pattern to awk, you have the problem that awk treats \ specially there (same happens for -F x which is similar to -v FS=x), so you'd need to escape the backslashes here.

It's better to use ENVIRON to pass arbitrary strings from shell to awk as that doesn't have that issue.

So:

pattern='"\."|0|1'
PATTERN=$pattern DELIMITER=$delimiter awk -n n="$n" '
  BEGIN {FS = ENVIRON["DELIMITER"]; m = ENVIRON["PATTERN"]}
  $n ~ "^(" m ")$" {...}'

(still using -v for n as those are expected to be numbers so without backslash).

Note the (, ) above. ^x|y$ would be either x at the start or y at the end.

If you want the pattern to match exactly on either "." (with quotes included), 0 or 1, you'd need the pattern to be ^("\."|[01])$ or ^("[.]"|[01])$ or ^("\."|0|1)$, etc.

But when using -v to pass that pattern to awk, you have the problem that awk treats \ specially there (same happens for -F x which is similar to -v FS=x), so you'd need to escape the backslashes here.

It's better to use ENVIRON to pass arbitrary strings from shell to awk as that doesn't have that issue.

So:

pattern='"\."|0|1'
PATTERN=$pattern DELIMITER=$delimiter awk -v n="$n" '
  BEGIN {FS = ENVIRON["DELIMITER"]; m = ENVIRON["PATTERN"]}
  $n ~ "^(" m ")$" {...}'

(still using -v for n as those are expected to be numbers so without backslash).

Note the (, ) above. ^x|y$ would be either x at the start or y at the end.

added 2 characters in body
Source Link
Stéphane Chazelas
  • 584.9k
  • 96
  • 1.1k
  • 1.7k

If you want the pattern to match exactly on either "." (with quotes included), 0 or 1, you'd need the pattern to be ^("\."|[01])$ or ^("[.]"|[01])$ or ^(\"\.|0|1"|0|1)$, etc.

But when using -v to pass that pattern to awk, you have the problem that awk treats \ specially there (same happens for -F x which is similar to -v FS=x), so you'd need to escape the backslashes here.

It's better to use ENVIRON to pass arbitrary strings from shell to awk as that doesn't have that issue.

So:

pattern='"\."|0|1'
PATTERN=$pattern DELIMITER=$delimiter awk -n n="$n" '
  BEGIN {FS = ENVIRON["DELIMITER"]; m = ENVIRON["PATTERN"]}
  $n ~ "^(" m ")$" {...}'

(still using -v for n as those are expected to be numbers so without backslash).

Note the (, ) above. ^x|y$ would be either x at the start or y at the end.

If you want the pattern to match exactly on either "." (with quotes included), 0 or 1, you'd need the pattern to be ^("\."|[01])$ or ^("[.]"|[01])$ or ^(\.|0|1)$, etc.

But when using -v to pass that pattern to awk, you have the problem that awk treats \ specially there (same happens for -F x which is similar to -v FS=x), so you'd need to escape the backslashes here.

It's better to use ENVIRON to pass arbitrary strings from shell to awk as that doesn't have that issue.

So:

pattern='"\."|0|1'
PATTERN=$pattern DELIMITER=$delimiter awk -n n="$n" '
  BEGIN {FS = ENVIRON["DELIMITER"]; m = ENVIRON["PATTERN"]}
  $n ~ "^(" m ")$" {...}'

(still using -v for n as those are expected to be numbers so without backslash).

Note the (, ) above. ^x|y$ would be either x at the start or y at the end.

If you want the pattern to match exactly on either "." (with quotes included), 0 or 1, you'd need the pattern to be ^("\."|[01])$ or ^("[.]"|[01])$ or ^("\."|0|1)$, etc.

But when using -v to pass that pattern to awk, you have the problem that awk treats \ specially there (same happens for -F x which is similar to -v FS=x), so you'd need to escape the backslashes here.

It's better to use ENVIRON to pass arbitrary strings from shell to awk as that doesn't have that issue.

So:

pattern='"\."|0|1'
PATTERN=$pattern DELIMITER=$delimiter awk -n n="$n" '
  BEGIN {FS = ENVIRON["DELIMITER"]; m = ENVIRON["PATTERN"]}
  $n ~ "^(" m ")$" {...}'

(still using -v for n as those are expected to be numbers so without backslash).

Note the (, ) above. ^x|y$ would be either x at the start or y at the end.

deleted 3 characters in body
Source Link
Stéphane Chazelas
  • 584.9k
  • 96
  • 1.1k
  • 1.7k

If you want the pattern to match exactly on either "." (with quotes included), 0 or 1, you'd need the pattern to be ^("\.")|[01]"|[01])$ or ^("[.]")|[01]]"|[01])$ or ^(\.|0|1)$, etc.

But when using -v to pass that pattern to awk, you have the problem that awk treats \ specially there (same happens for -F x which is similar to -v FS=x), so you'd need to escape the backslashes here.

It's better to use ENVIRON to pass arbitrary strings from shell to awk as that doesn't have that issue.

So:

pattern='"\."|[01]'"|0|1'
PATTERN=$pattern DELIMITER=$delimiter awk -n n="$n" '
  BEGIN {FS = ENVIRON["DELIMITER"]; m = ENVIRON["PATTERN"]}
  $n ~ "^(" m ")$" {...}'

(still using -v for n as those are expected to be numbers so without backslash).

Note the (, ) above. ^x|y$ would be either x at the start or y at the end.

If you want the pattern to match exactly on either "." (with quotes included), 0 or 1, you'd need the pattern to be ^("\.")|[01])$ or ^("[.]")|[01])$ or ^(\.|0|1)$, etc.

But when using -v to pass that pattern to awk, you have the problem that awk treats \ specially there (same happens for -F x which is similar to -v FS=x), so you'd need to escape the backslashes here.

It's better to use ENVIRON to pass arbitrary strings from shell to awk as that doesn't have that issue.

So:

pattern='"\."|[01]'
PATTERN=$pattern DELIMITER=$delimiter awk -n n="$n" '
  BEGIN {FS = ENVIRON["DELIMITER"]; m = ENVIRON["PATTERN"]}
  $n ~ "^(" m ")$" {...}'

(still using -v for n as those are expected to be numbers so without backslash).

Note the (, ) above. ^x|y$ would be either x at the start or y at the end.

If you want the pattern to match exactly on either "." (with quotes included), 0 or 1, you'd need the pattern to be ^("\."|[01])$ or ^("[.]"|[01])$ or ^(\.|0|1)$, etc.

But when using -v to pass that pattern to awk, you have the problem that awk treats \ specially there (same happens for -F x which is similar to -v FS=x), so you'd need to escape the backslashes here.

It's better to use ENVIRON to pass arbitrary strings from shell to awk as that doesn't have that issue.

So:

pattern='"\."|0|1'
PATTERN=$pattern DELIMITER=$delimiter awk -n n="$n" '
  BEGIN {FS = ENVIRON["DELIMITER"]; m = ENVIRON["PATTERN"]}
  $n ~ "^(" m ")$" {...}'

(still using -v for n as those are expected to be numbers so without backslash).

Note the (, ) above. ^x|y$ would be either x at the start or y at the end.

Source Link
Stéphane Chazelas
  • 584.9k
  • 96
  • 1.1k
  • 1.7k
Loading