Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print selected columns separated by tabs?

Tags:

shell

awk

I have a txt file with columns separated by tabs and based on that file, I want to create a new file that only contains information from some of the columns.

This is what I have now:

awk '{ print $1, $5 }' filename > newfilename  

That works except that when column 5 contains spaces e.g 123 Street, only 123 shows up and the street is considered as another column.

How can I achieve what I'm trying to do?

like image 363
teepusink Avatar asked Apr 14 '10 20:04

teepusink


2 Answers

You can specify the field separator as tab:

awk 'BEGIN { FS = "\t" } ; { print $1, $5 }' filename > newfilename 

Or from the command line like this:

awk -F"\t" '{ print $1, $5 }' filename > newfilename 
like image 165
Mark Byers Avatar answered Nov 02 '22 16:11

Mark Byers


What about simple cut shell comand?

very simple yet does the job

cut -d "\t" -f 1,5 filename > newfilename
like image 29
jppalencar Avatar answered Nov 02 '22 15:11

jppalencar