1

I have a file which has many lines, but each line is of short length.

How can I print that file in multiple column format in bash?

That file is like below:

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

As you can see, each line's width is short.

And I wish to print that file in below format:

$> 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
5
  • 2
    For every question about reorganizing data, I would like to recommend more powerful script language such as perl or python, not bash script. Reading in data into hash table and outputting them as any format as you like will be a more beautiful implementation than bash's. Commented Jul 4, 2016 at 7:39
  • 2
    I want to find a bash solution so then I can use it without installing anything or pull any code. Commented Jul 4, 2016 at 7:41
  • What are the requirements to get the output? How do you decide how many lines each column contains? Commented Jul 4, 2016 at 8:24
  • That doesn't matter. Just equally split line numbers into three will also do. Commented Jul 4, 2016 at 8:26
  • I just want a simple bash solution. Commented Jul 4, 2016 at 8:27

2 Answers 2

1

This script will work. It does:

  1. Determine longest line and longest paragraph

  2. Put each paragraph in a single file and add spaces to each line so it will contain as many chars as the longest line.

  3. Add new lines to the files until they are all the same length.

  4. Use tr to print them side by side.

This is a bash only script:

#!/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

With your input file it will output this:

./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
Sign up to request clarification or add additional context in comments.

Comments

0

Using pr can solve this problem.

$> 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

Arguments explanation:

  • pr -3: three columns

  • pr -t: no head and tail banner

  • pr -w 120: line length is of 120 char

  • expand: replace tabs into spaces

If you want the other direction you can use -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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.