0

Similar to

Append one column from fileB onto end of FileA and bash cut columns to one file and save onto the end of another file

However, in this case, I want the result to print to a file (file1.txt), not standard output (stdout).

Background // I have two tab-delimited files (both with 3 columns):

>file1.txt
4481679 4481722 .
4481791 4481820 .
4481947 4482268 .

>file2.txt
13368   -       366
13668   +       940
14016   -       270

Desired output // I want to merge/append the second column of file2.txt to file1.txt (final file has 4 columns):

>file1.txt
4481679 4481722 .       -
4481791 4481820 .       +
4481947 4482268 .       -

Errors // So far, I have tried the following (which all look correct--like the above), but each simply prints to stdout:

    Attempt1:
$ paste file1.txt <(cut -f2 file2.txt)
    Attempt2:
$ cut -f2 file2.txt | paste file1.txt -
    Attempt3:
$ paste file1.txt <(awk '{print $2}' file2.txt)

Also, the following prints "paste: write error: No space left on device paste: write error":

    Attempt4:
$ paste file1.txt <(cut -f2 file2.txt) >> file1.txt
    Attempt5:
$ cut -f2 file2.txt | paste file1.txt - > file3.txt

Any ideas how I can get this to work without using sponge from moreutils (i.e., using only things like paste, cut, awk, sed)?

Gawain
  • 147
  • 1
  • 15

1 Answers1

1

Use cut and paste like so:

paste file1.txt <(cut -f2 file2.txt) > out_file.txt

Your error relates to your disk being full. Delete extra files to free up some space.

Timur Shtatland
  • 9,559
  • 2
  • 24
  • 32
  • 1
    Ah, thank you for clarifying the disk space bit. Oddly, the output does seem to use a large amount of storage. However, as is, this code outputs *just* the second column to the ```out_file.txt```. I believe I need the ```>>``` instead of the singular ```>```. – Gawain Jan 07 '21 at 16:54