要仅获取0 hits found
尝试匹配该字符串但在零之前没有数字的行:
awk '$0 ~ /[^[:digit:]]0 hits found/' infile
假设一个测试输入文件 ( infile
) 像:
# Query: Name_ID1
# 2 hits found
# Query: Name_ID2
# 20 hits found
# Query: Name_ID3
# 0 hits found
# Query: Name_ID4
# 140 hits found
# Query: Name_ID5
# 0 hits found
# Query: Name_ID6
# 60 hits found
它产生:
# 0 hits found
# 0 hits found
对于第二个问题,用于getline
读取奇数行并同时打印两者,例如:
awk '{ getline hits_line; printf "%s %s\n", $0, hits_line }' infile
使用与以前相同的测试文件,它产生:
# Query: Name_ID1 # 2 hits found
# Query: Name_ID2 # 20 hits found
# Query: Name_ID3 # 0 hits found
# Query: Name_ID4 # 140 hits found
# Query: Name_ID5 # 0 hits found
# Query: Name_ID6 # 60 hits found
另外,我也喜欢使用vim尝试这种任务,所以这里有一个解决方案:
内容script.vim
:
set backup
for n in range( 1, line('$') / 2 )
execute "normal Jj"
endfor
saveas! Query2.txt
q!
像这样运行它:
vim -S script.vim infile
这将生成一个Query2.txt
包含内容的文件:
# Query: Name_ID1 # 2 hits found
# Query: Name_ID2 # 20 hits found
# Query: Name_ID3 # 0 hits found
# Query: Name_ID4 # 140 hits found
# Query: Name_ID5 # 0 hits found
# Query: Name_ID6 # 60 hits found