Linux - get text from second tab

Suppose we have a file:

sometext11 sometext12 sometext13 sometext21 sometext22 sometext23

The texts are separated by tabs, and we know sometext from column 1, but we want to get the text from column 2. I know that I can get the line:

grep 'sometext11' file.txt 

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

+4
source share
4 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 which line you are using grepping, you can use grep :

 grep -o 'sometext12' file.txt 
+11
source
 awk '{print $2}' < yourfile 
+2
source

You can use cut

 cut -f2 
+2
source

You do not need grep :

 awk '/sometext11/ {print $2}' file.txt 

or you can do it all in grep if your version supports compatible Perl regular expressions (PCRE) such as GNU or OS X grep :

 grep -Po '(?<=sometext11\t).*?(?=\t.*)' file.txt 
0
source

All Articles