尝试使用 cut...它又快又简单
首先你有重复的空格,如果你想要tr -s ' ',你可以将它们压缩到列之间的单个空格
如果每一列之间已经只有一个分隔符,您可以使用cut -d ' ' -f-2 打印字段(列)
例如,如果您的数据在文件 input.txt 中,您可以执行以下操作之一:
cat input.txt | tr -s ' ' | cut -d ' ' -f-2
或者,如果您通过删除第 3 列来更好地解释这个问题,您可以编写以下内容
cat input.txt | tr -s ' ' | cut -d ' ' --complement -f3
cut 非常强大,除了列之外,您还可以提取字节或字符范围
摘自手册页关于如何指定列表范围的语法
Each LIST is made up of one range, or many ranges separated by commas.
Selected input is written in the same order that it is read, and is
written exactly once. Each range is one of:
N N'th byte, character or field, counted from 1
N- from N'th byte, character or field, to end of line
N-M from N'th to M'th (included) byte, character or field
-M from first to M'th (included) byte, character or field
所以你也可以说你想要特定的第 1 列和第 2 列...
cat input.txt | tr -s ' ' | cut -d ' ' -f1,2