Reputation: 571
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.
Upvotes: 2
Views: 19471
Reputation: 9622
Solution 1:
paste fileA <(cut -f 2-3 fileB) > myNewFile
Solution 2:
paste fileA fileB | cut -f1-2,4-5
Upvotes: 3
Reputation: 5167
try this
paste fileA fileB | column -t | awk -F' ' '{print $1" "$2" "$4" "$5}'
or this
paste fileA fileB | column -s' ' -t | sed 's/ \+ /\t/g' | sed 's/\t/ /g' | cut -d' ' -f1-2,4-5
Upvotes: 0
Reputation: 1863
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
Upvotes: 2