How can I get 2nd and third column in tab delim file in bash?
Asked Answered
K

3

42

I want to use bash to process a tab delimited file. I only need the second column and third to a new file.

Kerrykersey answered 10/6, 2011 at 22:10 Comment(0)
P
86

cut(1) was made expressly for this purpose:

cut -f 2-3 input.txt > output.txt
Phenomena answered 10/6, 2011 at 22:13 Comment(0)
L
15

Cut is probably the best choice here, second to that is awk

awk -F"\t" '{print $2 "\t" $3}' input > out
Lecky answered 10/6, 2011 at 22:17 Comment(2)
the file had 2 million rows so i guess cut was good...thank you for your helpKerrykersey
awk comes in particularly handy when you want to change the order of columns. cut can't do that!Breezy
O
3

expanding on the answer of carl-norum, using only tab as a delimiter, not all blanks:

cut -d$'\t' -f 2-3 input.txt > output.txt

don't put a space between d and $

Offer answered 7/6, 2018 at 9:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.