\t' -f2 I have a string split by TAB ...">

How to split a line into a tab in bash

test1="one  two three   four    five"
echo $test1 | cut -d $'\t' -f2

      

I have a string split by TAB

and I want to get the second word with a command cut

.

I found the question How to split a string in bash with a tab delimiter . But the solution is not used with cut

.

+3


source to share


4 answers


This is because you need to quote $test1

when you echo

:

echo "$test1" | cut -d$'\t' -f2

      



Otherwise, the format disappears and the tabs are converted to spaces:

$ s="hello      bye     ciao"
$ echo "$s"              <--- quoting
hello   bye ciao
$ echo $s                <--- without quotes
hello bye ciao

      

+4


source


You don't need cut

and you can keep the fork:



$ test1=$(printf "one\ttwo\three\tfour\tfive")
$ read _ two _ <<< "${test1}"
$ echo "${two}"
two

      

+1


source


Try using cut

without option -d

:

echo "$test1" | cut -f2

      

Below is an Expert Advisor from the cut

reference page:

-d, --delimiter=DELIM
    use DELIM instead of TAB for field delimiter 

      

0


source


I am running this:

test1="one;two;three;four;five"

echo $test1 | cut -d \; -f2

      

and get:

two

      

and your example:

test1="one  two three   four    five"

echo $test1 | cut -d \t -f2

      

and get:

wo

      

Hope it helps.


This \ t is a problem I think.

-3


source







All Articles