【问题标题】:How to clean up misaligned columns in text separated by spaces? [duplicate]如何清理以空格分隔的文本中未对齐的列? [复制]
【发布时间】:2020-01-24 23:04:27
【问题描述】:

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

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

【问题讨论】:

标签: linux bash shell


【解决方案1】:

这很丑陋,但这可能有效:

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

【讨论】:

  • 或使用 GNU sed sed 's/ /\t/3' filesed 's/ /\t/3' file | column -s $'\t' -t
  • 如果我执行这个cat align.txt | ruby -nle 'puts $_.split().join("\t")' |pr --expand-tabs -tT| 命令,那么它会给我这一行>。下面是我得到的输出:host:~$ cat align.txt | ruby -nle 'puts $_.split().join("\t")' |pr --expand-tabs -tT| >
  • @Cyrus 我的文件中的第二个命令出现了段错误。不知道为什么?
  • @flash:可能是您正在使用的column 命令的实现中的一个错误。
猜你喜欢
  • 1970-01-01
  • 2020-01-14
  • 1970-01-01
  • 1970-01-01
  • 2018-09-01
  • 1970-01-01
  • 2021-06-01
  • 2019-11-24
  • 1970-01-01
相关资源
最近更新 更多