Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有类似的文件test.txt :
aa:bbbbbb:22.3 a:bb:33.2 a:bbbb:22.3 aaaa:bb:39.9
我知道如何计算和排序它们:
awk -F ':' '{print $2}' test.txt | awk '{print length($0),$0}' | sort -nr
现在我想从文件中删除第一行和第三行,因为这些行中第二个字段(包含“b”)的长度大于 3。如何使用 awk/sed 来做到这一点?谢谢。
与awk:
awk
这将输出第二个字段大于 3 的行:
$ awk -F: 'length($2)>3' file aa:bbbbbb:22.3 a:bbbb:22.3
做相反的事情:
$ awk -F: 'length($2)<=3' file a:bb:33.2 aaaa:bb:39.9
sed的代码:
sed '/.*:..:.*/!d' file
或更笼统地说:
sed '/.*:.\{2\}:.*/!d' file