AWK在搜索程序中

时间:2014-07-17 17:49:57

标签: awk

有没有人知道这段代码的含义

search_book()
{
    awk -F':' -v search="$1" '$1 ~ search || $2 ~ search { i++; printf "%s, %s,$%s,%s,%s\n", $1, $2, $3, $4, $5 } END { printf "%d records found\n", i }' books.txt
}
这是什么意思?谢谢

search="$1" '$1 ~ search || $2 ~ search { i++; printf "%s, %s,$%s,%s,%s\n", $1, $2, $3, $4, $5 }

1 个答案:

答案 0 :(得分:1)

一些信息

awk -F':' -v search="$1" '                              # Splite the indata by ":" , get the pattern to search from  "$1" in the bash       
$1 ~ search || $2 ~ search {                            # If field "#1" or "#2" does contain the search pattern do:
    i++                                                 # Increment variable "i"
    printf "%s, %s,$%s,%s,%s\n", $1, $2, $3, $4, $5 }   # and print field $1 to $5

{printf "%s, %s,$%s,%s,%s\n", $1, $2, $3, $4, $5 }可以更改为
{print $1," "$2,"$"$3,$4,$5}' OFS=,