Skip to main content
added 304 characters in body
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k

A shell variable is just that: a shell variable. If you want to turn it into a awk variable, you need a syntax such as:

awk -v x="$x" '$2 == x {print $1}' infile

or

awk '$2 == x {print $1}' x="$x" infile

However, those suffer from a problem: escape sequences are expanded in them (and with GNU awk 4.2 or above, if $x starts with @/ and ends in /, it's treated as a regexp type of variable).

So, for instance if the shell variable contains the two characters backslash and n, the awk variable will end up containing the newline character (and with gawk 4.2+, if it contains @/foo/, the awk variable will contain foo and be of type regexp).

Another approach (but which requires a POSIX awk or nawk (as opposed to the original awk as still found on a few odd Unices)) is to use environment variables:

x="$x" awk '$2 == ENVIRON["x"] {print $1}' infile

Another approach (still with newer awks) is to use the ARGV array in awk:

awk 'BEGIN {x = ARGV[1]; delete ARGV[1]}
  $2 == x {print $1}' "$x" infile

A shell variable is just that: a shell variable. If you want to turn it into a awk variable, you need a syntax such as:

awk -v x="$x" '$2 == x {print $1}' infile

or

awk '$2 == x {print $1}' x="$x" infile

However, those suffer from a problem: escape sequences are expanded in them.

So, for instance if the shell variable contains the two characters backslash and n, the awk variable will end up containing the newline character.

Another approach (but which requires a POSIX awk or nawk (as opposed to the original awk as still found on a few odd Unices)) is to use environment variables:

x="$x" awk '$2 == ENVIRON["x"] {print $1}' infile

Another approach (still with newer awks) is to use the ARGV array in awk:

awk 'BEGIN {x = ARGV[1]; delete ARGV[1]}
  $2 == x {print $1}' "$x" infile

A shell variable is just that: a shell variable. If you want to turn it into a awk variable, you need a syntax such as:

awk -v x="$x" '$2 == x {print $1}' infile

or

awk '$2 == x {print $1}' x="$x" infile

However, those suffer from a problem: escape sequences are expanded in them (and with GNU awk 4.2 or above, if $x starts with @/ and ends in /, it's treated as a regexp type of variable).

So, for instance if the shell variable contains the two characters backslash and n, the awk variable will end up containing the newline character (and with gawk 4.2+, if it contains @/foo/, the awk variable will contain foo and be of type regexp).

Another approach (but which requires a POSIX awk or nawk (as opposed to the original awk as still found on a few odd Unices)) is to use environment variables:

x="$x" awk '$2 == ENVIRON["x"] {print $1}' infile

Another approach (still with newer awks) is to use the ARGV array in awk:

awk 'BEGIN {x = ARGV[1]; delete ARGV[1]}
  $2 == x {print $1}' "$x" infile
typo
Source Link
αғsнιη
  • 41.9k
  • 17
  • 75
  • 117

A shell variable is just that: a shell variable. If you want to turn it into a awk variable, you need a syntax such as:

awk -v x="$x" '$2 == x {print $1}' infile

or

awk '$2 == x {print $1}' x="$x" infile

However, those suffer from a problem: escape sequences are expanded in them.

So, for instance if the shell variable contains the the two characters backslash and n, the awk variable will end up containing the newline character.

Another approach (but which requires a POSIX awk or nawk (as opposed to the original awk as still found on a few odd Unices)) is to use environment variables:

x="$x" awk '$2 == ENVIRON["x"] {print $1}' infile

Another approach (still with newer awks) is to use the ARGV array in awk:

awk 'BEGIN {x = ARGV[1]; delete ARGV[1]}
  $2 == x {print $1}' "$x" infile

A shell variable is just that: a shell variable. If you want to turn it into a awk variable, you need a syntax such as:

awk -v x="$x" '$2 == x {print $1}' infile

or

awk '$2 == x {print $1}' x="$x" infile

However, those suffer from a problem: escape sequences are expanded in them.

So, for instance if the shell variable contains the the two characters backslash and n, the awk variable will end up containing the newline character.

Another approach (but which requires a POSIX awk or nawk (as opposed to the original awk as still found on a few odd Unices)) is to use environment variables:

x="$x" awk '$2 == ENVIRON["x"] {print $1}' infile

Another approach (still with newer awks) is to use the ARGV array in awk:

awk 'BEGIN {x = ARGV[1]; delete ARGV[1]}
  $2 == x {print $1}' "$x" infile

A shell variable is just that: a shell variable. If you want to turn it into a awk variable, you need a syntax such as:

awk -v x="$x" '$2 == x {print $1}' infile

or

awk '$2 == x {print $1}' x="$x" infile

However, those suffer from a problem: escape sequences are expanded in them.

So, for instance if the shell variable contains the two characters backslash and n, the awk variable will end up containing the newline character.

Another approach (but which requires a POSIX awk or nawk (as opposed to the original awk as still found on a few odd Unices)) is to use environment variables:

x="$x" awk '$2 == ENVIRON["x"] {print $1}' infile

Another approach (still with newer awks) is to use the ARGV array in awk:

awk 'BEGIN {x = ARGV[1]; delete ARGV[1]}
  $2 == x {print $1}' "$x" infile
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k

A shell variable is just that: a shell variable. If you want to turn it into a awk variable, you need a syntax such as:

awk -v x="$x" '$2 == x {print $1}' infile

or

awk '$2 == x {print $1}' x="$x" infile

However, those suffer from a problem: escape sequences are expanded in them.

So, for instance if the shell variable contains the the two characters backslash and n, the awk variable will end up containing the newline character.

Another approach (but which requires a POSIX awk or nawk (as opposed to the original awk as still found on a few odd Unices)) is to use environment variables:

x="$x" awk '$2 == ENVIRON["x"] {print $1}' infile

Another approach (still with newer awks) is to use the ARGV array in awk:

awk 'BEGIN {x = ARGV[1]; delete ARGV[1]}
  $2 == x {print $1}' "$x" infile