如何在bash中以多列格式打印一个文件?

时间:2016-07-04 07:27:05

标签: bash

我有一个包含许多行的文件,但每行的长度都很短。

如何在bash中以多列格式打印该文件?

该文件如下所示:

This is the first line haha.
second line
third line

first line of second paragraph
hahahahahaha
yayayayayaya

This is the third paragraph.
3-1. random random random
3-2. random random
3-3. random random random
3-4. random random random random
3-5. random random random
3-6. random
3-7. random random random
3-8. random random
3-9. random random
3-10. random
3-11. random random random
3-12. random random
3-13. random

This is the fourth paragraph.
4-1. random random random
4-2. random random
4-3. random random random
4-4. random random random random
4-5. random random random
4-6. random
4-7. random random random
4-8. random random
4-9. random random
4-10. random
4-11. random random random
4-12. random random
4-13. random

如您所见,每条线的宽度都很短。

我希望以下面的格式打印该文件:

$> cat sample.txt -n 3
This is the first line haha.      This is the third paragraph.        This is the fourth paragraph.
second line                       3-1. random random random           4-1. random random random
third line                        3-2. random random                  4-2. random random
                                  3-3. random random random           4-3. random random random
first line of second paragraph    3-4. random random random random    4-4. random random random random
hahahahahaha                      3-5. random random random           4-5. random random random
yayayayayaya                      3-6. random                         4-6. random
                                  3-7. random random random           4-7. random random random
                                  3-8. random random                  4-8. random random
                                  3-9. random random                  4-9. random random
                                  3-10. random                        4-10. random
                                  3-11. random random random          4-11. random random random
                                  3-12. random random                 4-12. random random
                                  3-13. random                        4-13. random

2 个答案:

答案 0 :(得分:1)

此脚本可以使用。它确实:

  1. 确定最长行和最长段落

  2. 将每个段落放在一个文件中,并为每一行添加空格,使其包含与最长行一样多的字符。

  3. 在文件中添加新行,直到它们的长度相同。

  4. 使用tr并排打印。

  5. 这是一个仅限bash的脚本:

    #!/bin/bash
    #get input file name
    input="$1"
    #count paragraphs starting at 1
    paraNr=1
    #what is the longest line in the file
    longestLine=$(($(cat "$input" | wc -L) + 1))
    paraFile=$(tempfile)
    maxLength=0
    currentLength=0
    while read -r line;
    do
        if [ -z "$line" ]
        then
        paraNr=$((paraNr+1))
        if [[ "$currentLength" -gt "$maxLength" ]]
        then
            #determine longest paragraph to later put in empty lines in shorter paragaphs
            maxLength=$currentLength
            currentLength=0
        fi
        else
        length=${#line}
        paddLength=$(($longestLine-$length))
        padding=$(printf ' %.0s' $(seq 1 "$paddLength"))
        echo "$line$padding" >> $paraFile.$paraNr   
        currentLength=$((currentLength+1))
        fi    
    done < "$input"
    
    files=""
    #maxLength=0
    for i in $(seq 1 $paraNr)
    do
        current=$(cat "$paraFile"."$i" | wc -l)
        add=$(($maxLength-$current))
        for x in $(seq 0 $add)
        do
        padding=$(printf ' %.0s' $(seq 1 "$longestLine"))
        echo "$padding" >> $paraFile.$i
        done
        files="$files $paraFile.$i"
    done
    
    pr -t -J -m -S"" $files
    

    使用您的输入文件,它将输出:

    ./s.sh input.txt
    This is the first line haha.     first line of second paragraph   This is the third paragraph.     This is the fourth paragraph.
    second line                      hahahahahaha                     3-1. random random random        4-1. random random random
    third line                       yayayayayaya                     3-2. random random               4-2. random random
                                                                      3-3. random random random        4-3. random random random
                                                                      3-4. random random random random 4-4. random random random random
                                                                      3-5. random random random        4-5. random random random
                                                                      3-6. random                      4-6. random
                                                                      3-7. random random random        4-7. random random random
                                                                      3-8. random random               4-8. random random
                                                                      3-9. random random               4-9. random random
                                                                      3-10. random                     4-10. random
                                                                      3-11. random random random       4-11. random random random
                                                                      3-12. random random              4-12. random random
                                                                      3-13. random                     4-13. random
    

答案 1 :(得分:0)

使用pr可以解决此问题。

$> pr -t -3 -w 120 sample.txt | expand
This is the first line haha.            3-5. random random random               4-3. random random random
second line                             3-6. random                             4-4. random random random random
third line                              3-7. random random random               4-5. random random random
                                        3-8. random random                      4-6. random
first line of second paragraph          3-9. random random                      4-7. random random random
hahahahahaha                            3-10. random                            4-8. random random
yayayayayaya                            3-11. random random random              4-9. random random
                                        3-12. random random                     4-10. random
This is the third paragraph.            3-13. random                            4-11. random random random
3-1. random random random                                                       4-12. random random
3-2. random random                      This is the fourth paragraph.           4-13. random
3-3. random random random               4-1. random random random
3-4. random random random random        4-2. random random

参数说明:

  • pr -3:三列

  • pr -t:没有头尾标语

  • pr -w 120:行长度为120字符

  • expand:将标签替换为空格

如果您想要另一个方向,可以使用-l 1

$> pr -l 1 -t -3 -i -w 120 sample.txt | expand
This is the first line haha.            second line                             third line
                                        first line of second paragraph          hahahahahaha
yayayayayaya                                                                    This is the third paragraph.
3-1. random random random               3-2. random random                      3-3. random random random
3-4. random random random random        3-5. random random random               3-6. random
3-7. random random random               3-8. random random                      3-9. random random
3-10. random                            3-11. random random random              3-12. random random
3-13. random                                                                    This is the fourth paragraph.
4-1. random random random               4-2. random random                      4-3. random random random
4-4. random random random random        4-5. random random random               4-6. random
4-7. random random random               4-8. random random                      4-9. random random
4-10. random                            4-11. random random random              4-12. random random
4-13. random