Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux - get text from second tab

Tags:

linux

text

bash

Suppose that we have file like this:

sometext11 sometext12 sometext13 sometext21 sometext22 sometext23

Texts are separated by tabs and we know sometext from column 1 but want to get text from column 2. I know I can get line by:

grep 'sometext11' file.txt

How to get text from second column? Maybe some tool with option -t [column nr]?

like image 345
bandit Avatar asked Jun 21 '12 06:06

bandit


3 Answers

awk:

awk '{print $2}' file.txt

cut:

cut -f2 file.txt

bash:

while read -a A; do echo ${A[1]}; done < file.txt

perl:

perl -lane 'print $F[1]' file.txt

If you know the string you are grepping for, you can use grep:

grep -o 'sometext12' file.txt
like image 162
Fredrik Pihl Avatar answered Nov 07 '22 09:11

Fredrik Pihl


awk '{print $2}' < yourfile
like image 28
Gryphius Avatar answered Nov 07 '22 10:11

Gryphius


You can use cut command

cut -f2
like image 20
tuxuday Avatar answered Nov 07 '22 09:11

tuxuday