0

I have three text files and I want to combine them in one text file on my Linux machine.

Input files:

1_val.txt

0x0000000000060b7c02555b80
0x0000000000060b7c026c6580
0x0000000000060b7c0285ff80

2_val.txt

0x0000000000060b7c0257a180
0x0000000000060b7c026eab80
0x0000000000060b7c02884580

3_val.txt

2.2734
2.2734
2.2734

Expected output:

values.txt

0x0000000000060b7c02555b80 0x0000000000060b7c0257a180 2.2734
0x0000000000060b7c026c6580 0x0000000000060b7c026eab80 2.2734
0x0000000000060b7c0285ff80 0x0000000000060b7c02884580 2.2734

Output generated: The last three values in the first and second column are missing.

0x0000000000060b7c02555 0x0000000000060b7c0257a 2.2734
0x0000000000060b7c026c6 0x0000000000060b7c026ea 2.2734
0x0000000000060b7c0285f 0x0000000000060b7c02884 2.2734

The code I used to get this done,

pr -m -t 1_val.txt 2_val.txt 3_val.txt > values.txt
terdon
  • 234,489
  • 66
  • 447
  • 667
Basil
  • 63
  • 6

2 Answers2

4

Use paste and specify a space delimiter:

paste -d' ' 1_val.txt 2_val.txt 3_val.txt >values.txt

The problem is that the -m option of pr will truncate long lines by default (from man pr, emphasis mine):

-m, --merge

print all files in parallel, one in each column,
truncate lines, but join lines of full length with -J

In your case, this was cutting the last few characters of each line from your first file.

terdon
  • 234,489
  • 66
  • 447
  • 667
gogoud
  • 2,613
  • 2
  • 14
  • 18
  • 2
    @they actually, it turns out that this is the default behavior of `pr`: it will truncate long lines by default so the OP's result is reproducible using their command and works as expected with `paste`. – terdon Nov 30 '21 at 15:47
  • @gogoud that is a great explanation and it worked. – Basil Dec 01 '21 at 02:54
2

Use the -J switch to join the full lines and turn line truncation off.

pr -J -m -t 1_val.txt 2_val.txt 3_val.txt > values.txt

or set the page width to a large enough value:

pr -W80 -m -t 1_val.txt 2_val.txt 3_val.txt > values.txt
choroba
  • 45,735
  • 7
  • 84
  • 110