Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 475
RnD Avatar asked Jun 10 '11 22:06

RnD


People also ask

What is tab delimited in Linux?

`tab` is used as a separator In the tab-delimited file. This type of text file is created to store various types of text data in a structured format. Different types of command exist in Linux to parse this type of file.

How do I read a separated tab in Unix?

(The -r tells read that \ isn't special in the input data; the -a myArray tells it to split the input-line into words and store the results in myArray ; and the IFS=$'\t' tells it to use only tabs to split words, instead of the regular Bash default of also allowing spaces to split words as well.


2 Answers

cut(1) was made expressly for this purpose:

cut -f 2-3 input.txt > output.txt
like image 66
Carl Norum Avatar answered Oct 22 '22 19:10

Carl Norum


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

awk -F"\t" '{print $2 "\t" $3}' input > out
like image 15
Fredrik Pihl Avatar answered Oct 22 '22 19:10

Fredrik Pihl