Shell

根據行內容排列文本

  • July 6, 2022

是否可以插入新行以排列文件的內容?

我有:

1:1   Lorem ipsum dolor sit amet consectetur    1:1   This is sample text of varying length.
     adipiscing elit.                          1:2   This is another paragraph in this file.
1:2   Vivamus integer non suscipit taciti mus         Yet another sentence in this paragraph.
     etiam at primis tempor sagittis.          1:3   Another paragraph can be found here!

如何適當地添加空格以使數字對齊?

預期輸出:

1:1   Lorem ipsum dolor sit amet consectetur          This is sample text of varying length.
     adipiscing elit.                          
1:2   Vivamus integer non suscipit taciti mus         This is another paragraph in this file.    
     etiam at primis tempor sagittis.                Yet another sentence in this paragraph.
1:3                                                   Another paragraph can be found here!

編輯:由於線條會排列,因此無需重複數字,因此可以將其刪除。

POSIX 合規性是首選。

使用 GNUawk將文本作為固定寬度的記錄集讀取,其中每條記錄分為寬度為 6(左標籤)、42(文本左行)、6(右標籤)和 42(右行)的欄位文本):

BEGIN {
       FIELDWIDTHS = "6 42 6 42"
}

# New label seen on the left hand side.
# If this is a completely new label, then
# add it to the end of the "labels" array.
$1 != "      " {
       llabel = $1
       if (!seenlabels[llabel]) {
               labels[++n] = llabel
               seenlabels[llabel] = 1
       }
}

# Same as above, but for the right hand side.
$3 != "      " {
       rlabel = $3
       if (!seenlabels[rlabel]) {
               labels[++n] = rlabel
               seenlabels[rlabel] = 1
       }
}

# Add text to the labelled paragraphs, left and right,
# as strings delimited by ORS (newline).
{
       ltext[llabel] = (ltext[llabel] == "" ? $2 : ltext[llabel] ORS $2)
       rtext[rlabel] = (rtext[rlabel] == "" ? $4 : rtext[rlabel] ORS $4)
}

# At end, output.
END {
       # Iterate over all paragraphs (there are "n" of them).
       for (i = 1; i <= n; ++i) {
               delete llines
               delete rlines

               # Split the text for the left and right paragraph,
               # into arrays, "llines" and "rlines".
               a = split(ltext[labels[i]], llines, ORS)
               b = split(rtext[labels[i]], rlines, ORS)

               # The arrays may be of different lengths, but
               # "c" will be the length of the longest, i.e.
               # the number of lines of the paragraph to the
               # left or right, whichever is longes.
               c = (a > b ? a : b)

               # Print the first line of the left and right
               # of this paragarph (includes the label at the left).
               printf("%-6s%-42s%-6s%-42s\n", labels[i], llines[1], "", rlines[1])

               # Then print the other lines (no label).
               for (j = 2; j <= c; ++j)
                       printf("%-6s%-42s%-6s%-42s\n", "", llines[j], "", rlines[j])
       }
}

測試:

$ cat file
1:1   Lorem ipsum dolor sit amet consectetur    1:1   This is sample text of varying length.
     adipiscing elit.                          1:2   This is another paragraph in this file.
1:2   Vivamus integer non suscipit taciti mus         Yet another sentence in this paragraph.
     etiam at primis tempor sagittis.          1:3   Another paragraph can be found here!
$ gawk -f script file
1:1   Lorem ipsum dolor sit amet consectetur          This is sample text of varying length.
     adipiscing elit.
1:2   Vivamus integer non suscipit taciti mus         This is another paragraph in this file.
     etiam at primis tempor sagittis.                Yet another sentence in this paragraph.
1:3                                                   Another paragraph can be found here!

awk由於這是對(變數)的 POSIX 規範使用 GNU 特定的擴展FIELDWIDTHS,因此它不是嚴格的 POSIX 答案。

引用自:https://unix.stackexchange.com/questions/708861