How to use cut and paste together in one go

18,407

Solution 1

Solution 1:

paste fileA <(cut -f 2-3 fileB) > myNewFile

Solution 2:

paste fileA fileB | cut -f1-2,4-5

Solution 2

Sounds like you probably want the join or paste commands.

An example using paste to join ALL columns, then some column manipulation commands to filter the desired columns, taken from http://hintsforums.macworld.com/showthread.php?t=16618 is shown below:

$ cat foo
x1 y1
a1 b1
c1 d1
e1 f1

$ cat goo
x2 y2
a2 b2
c2 d2
e2 f2

$ paste foo goo
x1 y1   x2 y2
a1 b1   a2 b2
c1 d1   c2 d2
e1 f1   e2 f2

$ paste foo goo | column -t
x1  y1  x2  y2
a1  b1  a2  b2
c1  d1  c2  d2
e1  f1  e2  f2

$ paste foo goo | column -t | colrm 9 12
x1  y1  y2
a1  b1  b2
c1  d1  d2
e1  f1  f2
Share:
18,407
user1007742
Author by

user1007742

Updated on June 04, 2022

Comments

  • user1007742
    user1007742 almost 2 years

    I am trying to use cut and paste in the same command line but somehow it does not work. I have two files, fileA and fileB.

    fileA
    a b
    c d
    
    
    fileB
    1 2 3 4
    5 6 7 8
    

    I would like to cut the second and third column of fileB. I do this by the following command.

    cut -f 2-3 fileB
    

    Then in front of this, I would like to paste columns from fileA

    paste fileA | cut -f 2-3 fileB > myNewFile
    

    So myNewFile will look like

    a b 2 3
    c d 6 7
    

    I can do this in two lines.

    cut -f 2-3 fileB > part1
    paste fileA part1 > myNewFile
    

    But instead, i would like to do this in one go. Something similar to

    paste fileA | cut -f 2-3 fileB > myNewFile
    

    which does not work. It only prints the cut commands and do not do anything about the paste. How can i make this work in one command?

    Thanks.

  • agc
    agc about 8 years
    Given echo -e "a b\nc d" > fileA ; echo -e "1 2 3 4\n5 6 7 8" > fileB, specifying delimiters works better: paste -d ' ' file[AB] | cut -d ' ' -f 1,2,4,5