开发者

Match in awk disregarding white space

 1 #!/bin/bash
 2 KEY_FILE="keys"
 3 TABLE_FILE="table" #pipe delimited data
 4 
 5 i=0;
 6 while read key #print out rows in table file with key in keys file
 7 do
 8   let i=i+1
 9   #开发者_如何学Go key is first column in table
10   # print status to stderr
11   (echo "$KEY_FILE : line $i" >&2)
12   awk -F '|' "\$1 == $key {print \$0}" $TABLE_FILE
13 done < $KEY_FILE
14 

From line 12, will awk match the first column against the key if there is a white space discrepancy?


No, because you set your field separator. Whitespace characters are now significant.

You can set your field separator to be a regular expression to slurp up whitespace. Also, pass the value of the shell variable into an awk variable to avoid quoting hell.

gawk --posix -F '[[:space:]]*\|[[:space:]]*' -v k=$key '$1 == k' $TABLE_FILE

Note that {print $0} is the default action, so that can be omitted.

Also, awk can handle two files, so you don't need the bash loop:

gawk --posix -F '[[:space:]]*\|[[:space:]]*' '
  NR == FNR {key[$1] = ""; next}
  ($1 in key)
' $KEY_FILE $TABLE_FILE

The expression NR == FNR means that awk is looking at the first file in the list of files (NR is the "total record number" and FNR is the record number of the current file: they will only be the same for the first file). This program saves the keys in the key array, and the prints the records in the table file that have a key in that array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜