[英]awk does not print line number
I have the following code 我有以下代码
awk -v bookName="$bookName" -v authorName="$authorName" '/bookName:authorName/{print NR}' BookDB.txt
My bookDB.txt contains text with syntax as shown: 我的bookDB.txt包含具有如下语法的文本:
bookname:authorname:price 书名:作者名:价格
The code above aims to match exactly "bookname:authorname" and print out the line number where it match. 上面的代码旨在精确匹配“ bookname:authorname”并在匹配的位置打印出行号。 However, it is not printing out anything.
但是,它没有打印出任何东西。
I don't find anything wrong in the code syntax. 我在代码语法中没有发现任何错误。 Any help will be greatly appreciated.
任何帮助将不胜感激。
The issue is that you're trying to use awk variables within / /
, which you can't do. 问题是您试图在
/ /
使用awk变量,但您不能这样做。
Change your condition to $0 ~ bookName ":" authorName
to search the whole record $0
for the pattern. 将条件更改为
$0 ~ bookName ":" authorName
以在整个记录$0
搜索该模式。
Alternatively, if the fields in your file are separated by :
, you could also set the input field separator using -F:
and then use string comparisons on the first two fields: 或者,如果文件中的字段用
:
分隔,则还可以使用-F:
设置输入字段分隔符,然后在前两个字段上使用字符串比较:
awk -F: -v book="$bookName" -v author="$authorName" '
$1 == book && $2 == author{print NR}' BookDB.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.