alv*_*vas 1 unix bash tab-delimited
我有一个制表符分隔文件:
xyz
abc foo bar
hello world
lmn opq rst
Run Code Online (Sandbox Code Playgroud)
我想用3个标签提取线条并实现:
abc foo bar
lmn opq rst
Run Code Online (Sandbox Code Playgroud)
我一直在使用python:
fout = ('outfile', 'w')
with open('infile', 'r') as fin:
for line in fin:
if line.count('\t') == 3:
print>>fout, line
Run Code Online (Sandbox Code Playgroud)
什么是unix/bash方式呢?
您可以使用awk字段分隔符作为\t(制表符):
awk -F '\t' 'NF==3' file
abc foo bar
lmn opq rst
Run Code Online (Sandbox Code Playgroud)
NF==3 条件只打印正好3个字段的行(2个制表符).