用 awk / bash 包装一个超大列(漂亮的打印)

Wrap a single oversize column with awk / bash (pretty print)

我有这个 table 结构(假设分隔符是制表符):

AAA  BBBB  CCC
 01  Item  Description here
 02  Meti  A very very veeeery long description which will easily extend the recommended output width of 80 characters.
 03  Etim  Last description

我要的是这个:

AAA  BBBB  CCC
 01  Item  Description here
 02  Meti  A very very veeeery
           long description which
           will easily extend the
           recommended output width
           of 80 characters.
 03  Etim  Last description

这意味着我想将 </code> 拆分为具有预定义 <code>WIDTH 的字符串数组,其中第一个元素附加 "normally" 到当前行,所有后续元素都得到一个根据前两列的填充确定新的线宽(如果这样更容易,也可以固定填充)。

或者,[=15=] 中的文本可以被 GLOBAL_WIDTH(例如 80 个字符)拆分为第一个字符串,然后 "rest" -> 第一个字符串被打印 "normally"使用 printf,其余部分被 GLOBAL_WIDTH - (COLPAD1 + COLPAD2) 拆分并附加宽度新行,如上所示。

我尝试在我的 awk 格式化后使用 fmtfold(这基本上只是将标题添加到 table),但它们当然不反映 awk 的领域感知.

如何使用 bash 工具和/或 awk 实现此目的?

首先构建一个测试文件(名为file.txt):

echo "AA  BBBB  CCC
01  Item  Description here
02  Meti  A very very veeeery long description which will easily extend the recommended output width of 80 characters.
03  Etim  Last description" > file.txt

现在脚本(称为./split-columns.sh):

#!/bin/bash
FILE=

#find position of 3rd column (starting with 'CCC')
padding=`cat $FILE | head -n1 |  grep -aob 'CCC' | grep -oE '[0-9]+'`
paddingstr=`printf "%-${padding}s" ' '`

#set max length
maxcolsize=50
maxlen=$(($padding + $maxcolsize))

cat $FILE | while read line; do 
  #split the line only if it exceeds the desired length
  if [[ ${#line} -gt $maxlen ]] ; then 
    echo "$line" | fmt -s -w$maxcolsize - | head -n1
    echo "$line" | fmt -s -w$maxcolsize - | tail -n+2 | sed "s/^/$paddingstr/"
  else
    echo "$line";
  fi; 
done;

最后 运行 它将文件作为一个参数

./split-columns.sh file.txt > fixed-width-file.txt

输出将是:

AA  BBBB  CCC
01  Item  Description here
02  Meti  A very very veeeery long description
          which will easily extend the recommended output
          width of 80 characters.
03  Etim  Last description

你可以试试Perl one-liner

perl -lpe ' s/(.{20,}?)\s/\n\t   /g ' file

使用给定的输入

$ cat thurse.txt
AAA  BBBB  CCC
 01  Item  Description here
 02  Meti  A very very veeeery long description which will easily extend the recommended output width of 80 characters.
 03  Etim  Last description

$ perl -lpe ' s/(.{20,}?)\s/\n\t   /g ' thurse.txt
AAA  BBBB  CCC
 01  Item  Description
           here
 02  Meti  A very very
           veeeery long description
           which will easily extend
           the recommended output
           width of 80 characters.
 03  Etim  Last description

$

如果你想尝试长度 window 30/40/50

$ perl -lpe ' s/(.{30,}?)\s/\n\t   /g ' thurse.txt
AAA  BBBB  CCC
 01  Item  Description here
 02  Meti  A very very veeeery
           long description which will easily
           extend the recommended output width
           of 80 characters.
 03  Etim  Last description

$ perl -lpe ' s/(.{40,}?)\s/\n\t   /g ' thurse.txt
AAA  BBBB  CCC
 01  Item  Description here
 02  Meti  A very very veeeery long description
           which will easily extend the recommended
           output width of 80 characters.
 03  Etim  Last description

$ perl -lpe ' s/(.{50,}?)\s/\n\t   /g ' thurse.txt
AAA  BBBB  CCC
 01  Item  Description here
 02  Meti  A very very veeeery long description which
           will easily extend the recommended output width of
           80 characters.
 03  Etim  Last description

$