0

I'm trying to show multiple files to a user within less. These files have relatively long lines and contain the same text but in different languages (differences in line length to be expected).

Example:

file1.txt

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut lectus arcu bibendum at.

file2.txt (above but Google Translated)

The pain itself is the love of the pain, the main ecological problems, but I give this kind of time to fall down, so that some great pain and pain.
To drink at the bed of the bow.

Expected result:

Lorem ipsum dolor sit amet, consectetur adipiscing    The pain itself is the love of the pain, the main
elit, sed do eiusmod tempor incididunt ut labore et   ecological problems, but I give this kind of time
dolore magna aliqua.                                  to fall down, so that some great pain and pain.
Ut lectus arcu bibendum at.                           To drink at the bed of the bow.

In the case that one line is shorter than its corresponding line and doesn't wrap, the remaining space should be kept empty. This should work with 2 and 3 files and change the divider to suit the screen size. I've tried to use paste with column and pr to make it work. POSIX compliance is preferred.

paste file1.txt file2.txt | column -t -s $'\t'

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua.                       The pain itself is the love of the pain, the main ecological
problems, but I give this kind of time to fall down, so that some great pain and pain.
Ut lectus arcu bibendum at.                To drink at the bed of the bow.

The issue is that the line from the first file isn't line wrapped when it reaches the centre of the terminal while the second file does line wrap, but it starts at the beginning of the line rather than the centre.

Any help is appreciated

1 Answer 1

1

Do the line-wrapping first with fold

paste <(fold -sw 40 file1.txt) <(fold -sw 40 file2.txt) | column -t -s $'\t'

If you want it to adjust to your terminal size, do something like

width=$(( (COLUMNS - 4) / 2))
paste <(fold -sw $width file1.txt) <(fold -sw $width file2.txt) | column -t -s $'\t'

To wrap lines side-by-side, we'll have to go line-by-line pairwise through the files:

while IFS= read -r -u3 line1
      IFS= read -r -u4 line2
do
    paste <(fold -sw $width <<<"$line1") \
          <(fold -sw $width <<<"$line2")

done 3< file1.txt 4< file2.txt \
| column -t -s $'\t'

Given your sample input files, and a width of 45, this produces

Lorem ipsum dolor sit amet, consectetur       The pain itself is the love of the pain, the
adipiscing elit, sed do eiusmod tempor        main ecological problems, but I give this
incididunt ut labore et dolore magna aliqua.  kind of time to fall down, so that some
great pain and pain.
Ut lectus arcu bibendum at.                   To drink at the bed of the bow.

At this point, we're beyond POSIX shell: that's bash up there. We should be thinking about using a different programming language too.

4
  • This does work but because the line wrapping is done separately, if a line in file1.txt spills onto the next line but the corresponding line in file2.txt doesn't, there is no space. Edit: more specifically, all lines should stay aligned with each other and if one line wraps but its corresponding line doesn't the whole text is offset.
    – Venem
    Commented Jul 6, 2022 at 15:39
  • Are there any alternative methods? Even if line wrapping were to be done first, it would lead to issues since the actual files are in a tsv format and contain information about the text (like chapters and paragraph numbers) which are removed later but would mess up the line wrapping.
    – Venem
    Commented Jul 6, 2022 at 16:33
  • @Verem Didn't you already get an answer addressing that edge case? unix.stackexchange.com/a/708871/29793
    – r_31415
    Commented Jul 8, 2022 at 3:07
  • Yeah sorry I asked that question after this one so I gave up on this approach
    – Venem
    Commented Jul 11, 2022 at 14:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .