在Linux中,如何对齐包含不同分隔符的文本文件的不同部分?

时间:2019-11-09 14:28:58

标签: linux bash perl

我有一个文件,如下所示,

TypeError: can't pickle _tkinter.tkapp objects

我想将其与两个定界符(即b | b | c aaaaaaaa | b | c 1,2,3 10000,3,4, |)对齐。预期的输出文件是

,

可以使用任何简单的命令来完成吗?命令不应干扰文件中的其他文本(不包含该分隔符)。

1 个答案:

答案 0 :(得分:2)

首先,您需要找到每列的宽度。一旦有了它,就可以轻松创建/* My assumptions: 1. There is no guarantee they will be in sorted order and/or grouped together. 2. You can have at most two instances of the same value, meaning it's fine if I have one instance. I feel like the fact that there can be at most two instances of the same value means that it can be done in O(N) space and O(1) time... Thinking... */ // Naive first solution using more space. O(N) time and O(N) space. void deduplicatePartial(std::vector<int>& vec) { vector<int> resVec; resVec.reserve(vec.size()); unordered_map<int, int> numMap; for (int i{}; i < vec.size(); ++i) { auto iter = numMap.find(vec[i]); if (iter == numMap.end()) { // If I can't find this number in my map, resVec.push_back(vec[i]); // put it in my result vector. numMap[vec[i]] = {1}; // Make entry to map. } } vec = std::move(resVec); // Rip its guts out. //vec = {resVec.begin(), resVec.end()}; } 格式。

sprintf

在第二种情况下,需要在格式宽度中包含逗号的空格,并且需要在除最后一行之外的每个列值中添加逗号。

use List::Util qw( max );

my @a = (
    [qw( b b c )],
    [qw( aaaaaaaa b c )],
);

my @col_widths =
   map {
      my $col_idx = $_;
      max
         map { length($a[$_][$col_idx]) }
            0..$#a
   }
      0..$#{ $a[0] };

# 8, 1, 1 => "%-8s | %-1s | %s"
my $format =
   join " | ",
      map("%-${_}s", @col_widths[ 0..$#col_widths-1 ]),
      "%s";

say sprintf($format, @$_)
   for @a;

解决方案假定每一行具有相同的列数。进行必要的调整。

相关问题