如何清理以空格分隔的文本中未对齐的列?



我有一个文件,其中包含以下未对齐的字符串。我想正确对齐此文件,以便每行中的每个单词都正确间隔。

3 281 901.188.30.53 901001 1 poihelloswqs-1146414
3 598 901.189.166.233 901001 1 poihelloswqs-90877846
3 300 901.156.77.57 901001 1 poihelloswqs-90137229
3 263 901.156.17.80 901001 1 poihelloswqs-90135797
3 264 901.875.875.79 901001 1 poihelloswqs-1389375
3 265 901.189.153.234 901001 1 poihelloswqs-1568332
3 266 901.218.93.873 901001 1 poihelloswqs-3240561
3 268 901.158.76.23 901001 1 poihelloswqs-3242066
3 269 901.218.30.120 901001 1 poihelloswqs-3242532

它应该输出如下内容: 这样它们都正确对齐。这在Linux中可以做到吗?

3 281 901.188.30.53     901001 1 poihelloswqs-1146414
3 598 901.189.166.233   901001 1 poihelloswqs-90877846
3 300 901.156.77.57     901001 1 poihelloswqs-90137229
3 263 901.156.17.80     901001 1 poihelloswqs-90135797
3 264 901.875.875.79    901001 1 poihelloswqs-1389375
3 265 901.189.153.234   901001 1 poihelloswqs-1568332
3 266 901.218.93.873    901001 1 poihelloswqs-3240561
3 268 901.158.76.23     901001 1 poihelloswqs-3242066
3 269 901.218.30.120    901001 1 poihelloswqs-3242532

它非常丑陋,但这可能有效:

cat xx | ruby -nle 'puts $_.split().join("t")' |pr --expand-tabs -tT

如果列的宽度差异很大,则可能需要将单个t替换为多个制表符。

这是 ruby 在任何空白处拆分线条,然后使用制表符连接。最后pr用空格替换制表符(-tT避免了烦人的标题和分页(

如果您知道字段由单个字符分隔tr可以轻松进行替换:

cat xx | tr ' ' "t"  |pr --expand-tabs -tT

最新更新