splitting a line into array in bash with tab as delimiter
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have a file in the following format and it is tab separated
a k testis adult male 8 week rRNA
b k testis adult male 8 week rRNA
c k testis adult male 8 week rRNA
I want to do some operation on each line so I am using a while loop.I want to split each line on tab and then store let's say 6th column which is 8 week
in a variable. I am using this code but I am not able to get what I want
while read -r line; do tmp=($line///); col6=$tmp[5]; echo "$col6"; done < file.txt
This gives me 8
and not 8 week
. 8 week has a space in between 8 and week and hence I want to split the line on tab.
bash array
 |Â
show 6 more comments
up vote
1
down vote
favorite
I have a file in the following format and it is tab separated
a k testis adult male 8 week rRNA
b k testis adult male 8 week rRNA
c k testis adult male 8 week rRNA
I want to do some operation on each line so I am using a while loop.I want to split each line on tab and then store let's say 6th column which is 8 week
in a variable. I am using this code but I am not able to get what I want
while read -r line; do tmp=($line///); col6=$tmp[5]; echo "$col6"; done < file.txt
This gives me 8
and not 8 week
. 8 week has a space in between 8 and week and hence I want to split the line on tab.
bash array
I suppose that you want to save each 6th field value into an array, not a variable, right?
â RomanPerekhrest
Dec 13 '17 at 18:14
the 6th field is 8 week. I want to save that in a variable
â user3138373
Dec 13 '17 at 18:15
then, your title contradicts with your description. Update your question
â RomanPerekhrest
Dec 13 '17 at 18:16
1
I can't see what the contradiction is? They want to split the line, pick one of the fields and put it in a variable.
â ilkkachu
Dec 13 '17 at 18:21
As a general rule, you really don't want to use the shell for text parsing. It is very hard to do correctly (as you're finding out) and will be very slow. Have a look at Why is using a shell loop to process text considered bad practice? for more details and How can I extract/change lines in a text file whose data are separated into fields? for other options.
â terdonâ¦
Dec 13 '17 at 18:22
 |Â
show 6 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a file in the following format and it is tab separated
a k testis adult male 8 week rRNA
b k testis adult male 8 week rRNA
c k testis adult male 8 week rRNA
I want to do some operation on each line so I am using a while loop.I want to split each line on tab and then store let's say 6th column which is 8 week
in a variable. I am using this code but I am not able to get what I want
while read -r line; do tmp=($line///); col6=$tmp[5]; echo "$col6"; done < file.txt
This gives me 8
and not 8 week
. 8 week has a space in between 8 and week and hence I want to split the line on tab.
bash array
I have a file in the following format and it is tab separated
a k testis adult male 8 week rRNA
b k testis adult male 8 week rRNA
c k testis adult male 8 week rRNA
I want to do some operation on each line so I am using a while loop.I want to split each line on tab and then store let's say 6th column which is 8 week
in a variable. I am using this code but I am not able to get what I want
while read -r line; do tmp=($line///); col6=$tmp[5]; echo "$col6"; done < file.txt
This gives me 8
and not 8 week
. 8 week has a space in between 8 and week and hence I want to split the line on tab.
bash array
edited Dec 13 '17 at 18:58
Jake Symons
11918
11918
asked Dec 13 '17 at 18:02
user3138373
79641330
79641330
I suppose that you want to save each 6th field value into an array, not a variable, right?
â RomanPerekhrest
Dec 13 '17 at 18:14
the 6th field is 8 week. I want to save that in a variable
â user3138373
Dec 13 '17 at 18:15
then, your title contradicts with your description. Update your question
â RomanPerekhrest
Dec 13 '17 at 18:16
1
I can't see what the contradiction is? They want to split the line, pick one of the fields and put it in a variable.
â ilkkachu
Dec 13 '17 at 18:21
As a general rule, you really don't want to use the shell for text parsing. It is very hard to do correctly (as you're finding out) and will be very slow. Have a look at Why is using a shell loop to process text considered bad practice? for more details and How can I extract/change lines in a text file whose data are separated into fields? for other options.
â terdonâ¦
Dec 13 '17 at 18:22
 |Â
show 6 more comments
I suppose that you want to save each 6th field value into an array, not a variable, right?
â RomanPerekhrest
Dec 13 '17 at 18:14
the 6th field is 8 week. I want to save that in a variable
â user3138373
Dec 13 '17 at 18:15
then, your title contradicts with your description. Update your question
â RomanPerekhrest
Dec 13 '17 at 18:16
1
I can't see what the contradiction is? They want to split the line, pick one of the fields and put it in a variable.
â ilkkachu
Dec 13 '17 at 18:21
As a general rule, you really don't want to use the shell for text parsing. It is very hard to do correctly (as you're finding out) and will be very slow. Have a look at Why is using a shell loop to process text considered bad practice? for more details and How can I extract/change lines in a text file whose data are separated into fields? for other options.
â terdonâ¦
Dec 13 '17 at 18:22
I suppose that you want to save each 6th field value into an array, not a variable, right?
â RomanPerekhrest
Dec 13 '17 at 18:14
I suppose that you want to save each 6th field value into an array, not a variable, right?
â RomanPerekhrest
Dec 13 '17 at 18:14
the 6th field is 8 week. I want to save that in a variable
â user3138373
Dec 13 '17 at 18:15
the 6th field is 8 week. I want to save that in a variable
â user3138373
Dec 13 '17 at 18:15
then, your title contradicts with your description. Update your question
â RomanPerekhrest
Dec 13 '17 at 18:16
then, your title contradicts with your description. Update your question
â RomanPerekhrest
Dec 13 '17 at 18:16
1
1
I can't see what the contradiction is? They want to split the line, pick one of the fields and put it in a variable.
â ilkkachu
Dec 13 '17 at 18:21
I can't see what the contradiction is? They want to split the line, pick one of the fields and put it in a variable.
â ilkkachu
Dec 13 '17 at 18:21
As a general rule, you really don't want to use the shell for text parsing. It is very hard to do correctly (as you're finding out) and will be very slow. Have a look at Why is using a shell loop to process text considered bad practice? for more details and How can I extract/change lines in a text file whose data are separated into fields? for other options.
â terdonâ¦
Dec 13 '17 at 18:22
As a general rule, you really don't want to use the shell for text parsing. It is very hard to do correctly (as you're finding out) and will be very slow. Have a look at Why is using a shell loop to process text considered bad practice? for more details and How can I extract/change lines in a text file whose data are separated into fields? for other options.
â terdonâ¦
Dec 13 '17 at 18:22
 |Â
show 6 more comments
1 Answer
1
active
oldest
votes
up vote
7
down vote
accepted
The array assignment tmp=($line///)
splits the value on whatever characters IFS
contains, which by default includes tabs, and spaces and newlines. (I don't see what the empty substitution does.) To split only on tabs, set IFS
to this:
foo=$'atktestistadulttmalet8 weektRNA'
IFS=$'t'
tmp=($foo)
echo "$tmp[5]"
Though that still leaves globbing as an issue, and since you are already using while read
, you could use read -a tmp
(at least in Bash), it splits the input line based on IFS
, and separates the fields to elements of the named array:
$ while IFS=$'t' read -r -a tmp ; do
echo "$tmp[5]"
done <<< $'atktestistadulttmalet8 weektRNA'
That prints 8 week
. The other upside with this is that the change in IFS
is only in effect for the duration of the read
, not for the rest of the script.
Of course, if we know the number/meaning of the fields, we could just have read
split them to separate named variables:
... IFS=$'t' read -r col1 col2 col3 ...
Or, if you only want to print that one column, use cut
:
cut -d$'t' -f 6 < file.txt
can you tell what -a tmp is doing??
â user3138373
Dec 13 '17 at 18:20
2
@user3138373 the-a
flag tellsread
to store what it reads as an array.
â terdonâ¦
Dec 13 '17 at 18:23
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
accepted
The array assignment tmp=($line///)
splits the value on whatever characters IFS
contains, which by default includes tabs, and spaces and newlines. (I don't see what the empty substitution does.) To split only on tabs, set IFS
to this:
foo=$'atktestistadulttmalet8 weektRNA'
IFS=$'t'
tmp=($foo)
echo "$tmp[5]"
Though that still leaves globbing as an issue, and since you are already using while read
, you could use read -a tmp
(at least in Bash), it splits the input line based on IFS
, and separates the fields to elements of the named array:
$ while IFS=$'t' read -r -a tmp ; do
echo "$tmp[5]"
done <<< $'atktestistadulttmalet8 weektRNA'
That prints 8 week
. The other upside with this is that the change in IFS
is only in effect for the duration of the read
, not for the rest of the script.
Of course, if we know the number/meaning of the fields, we could just have read
split them to separate named variables:
... IFS=$'t' read -r col1 col2 col3 ...
Or, if you only want to print that one column, use cut
:
cut -d$'t' -f 6 < file.txt
can you tell what -a tmp is doing??
â user3138373
Dec 13 '17 at 18:20
2
@user3138373 the-a
flag tellsread
to store what it reads as an array.
â terdonâ¦
Dec 13 '17 at 18:23
add a comment |Â
up vote
7
down vote
accepted
The array assignment tmp=($line///)
splits the value on whatever characters IFS
contains, which by default includes tabs, and spaces and newlines. (I don't see what the empty substitution does.) To split only on tabs, set IFS
to this:
foo=$'atktestistadulttmalet8 weektRNA'
IFS=$'t'
tmp=($foo)
echo "$tmp[5]"
Though that still leaves globbing as an issue, and since you are already using while read
, you could use read -a tmp
(at least in Bash), it splits the input line based on IFS
, and separates the fields to elements of the named array:
$ while IFS=$'t' read -r -a tmp ; do
echo "$tmp[5]"
done <<< $'atktestistadulttmalet8 weektRNA'
That prints 8 week
. The other upside with this is that the change in IFS
is only in effect for the duration of the read
, not for the rest of the script.
Of course, if we know the number/meaning of the fields, we could just have read
split them to separate named variables:
... IFS=$'t' read -r col1 col2 col3 ...
Or, if you only want to print that one column, use cut
:
cut -d$'t' -f 6 < file.txt
can you tell what -a tmp is doing??
â user3138373
Dec 13 '17 at 18:20
2
@user3138373 the-a
flag tellsread
to store what it reads as an array.
â terdonâ¦
Dec 13 '17 at 18:23
add a comment |Â
up vote
7
down vote
accepted
up vote
7
down vote
accepted
The array assignment tmp=($line///)
splits the value on whatever characters IFS
contains, which by default includes tabs, and spaces and newlines. (I don't see what the empty substitution does.) To split only on tabs, set IFS
to this:
foo=$'atktestistadulttmalet8 weektRNA'
IFS=$'t'
tmp=($foo)
echo "$tmp[5]"
Though that still leaves globbing as an issue, and since you are already using while read
, you could use read -a tmp
(at least in Bash), it splits the input line based on IFS
, and separates the fields to elements of the named array:
$ while IFS=$'t' read -r -a tmp ; do
echo "$tmp[5]"
done <<< $'atktestistadulttmalet8 weektRNA'
That prints 8 week
. The other upside with this is that the change in IFS
is only in effect for the duration of the read
, not for the rest of the script.
Of course, if we know the number/meaning of the fields, we could just have read
split them to separate named variables:
... IFS=$'t' read -r col1 col2 col3 ...
Or, if you only want to print that one column, use cut
:
cut -d$'t' -f 6 < file.txt
The array assignment tmp=($line///)
splits the value on whatever characters IFS
contains, which by default includes tabs, and spaces and newlines. (I don't see what the empty substitution does.) To split only on tabs, set IFS
to this:
foo=$'atktestistadulttmalet8 weektRNA'
IFS=$'t'
tmp=($foo)
echo "$tmp[5]"
Though that still leaves globbing as an issue, and since you are already using while read
, you could use read -a tmp
(at least in Bash), it splits the input line based on IFS
, and separates the fields to elements of the named array:
$ while IFS=$'t' read -r -a tmp ; do
echo "$tmp[5]"
done <<< $'atktestistadulttmalet8 weektRNA'
That prints 8 week
. The other upside with this is that the change in IFS
is only in effect for the duration of the read
, not for the rest of the script.
Of course, if we know the number/meaning of the fields, we could just have read
split them to separate named variables:
... IFS=$'t' read -r col1 col2 col3 ...
Or, if you only want to print that one column, use cut
:
cut -d$'t' -f 6 < file.txt
edited Dec 13 '17 at 20:44
terdonâ¦
122k28230403
122k28230403
answered Dec 13 '17 at 18:15
ilkkachu
49.9k674137
49.9k674137
can you tell what -a tmp is doing??
â user3138373
Dec 13 '17 at 18:20
2
@user3138373 the-a
flag tellsread
to store what it reads as an array.
â terdonâ¦
Dec 13 '17 at 18:23
add a comment |Â
can you tell what -a tmp is doing??
â user3138373
Dec 13 '17 at 18:20
2
@user3138373 the-a
flag tellsread
to store what it reads as an array.
â terdonâ¦
Dec 13 '17 at 18:23
can you tell what -a tmp is doing??
â user3138373
Dec 13 '17 at 18:20
can you tell what -a tmp is doing??
â user3138373
Dec 13 '17 at 18:20
2
2
@user3138373 the
-a
flag tells read
to store what it reads as an array.â terdonâ¦
Dec 13 '17 at 18:23
@user3138373 the
-a
flag tells read
to store what it reads as an array.â terdonâ¦
Dec 13 '17 at 18:23
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f410710%2fsplitting-a-line-into-array-in-bash-with-tab-as-delimiter%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I suppose that you want to save each 6th field value into an array, not a variable, right?
â RomanPerekhrest
Dec 13 '17 at 18:14
the 6th field is 8 week. I want to save that in a variable
â user3138373
Dec 13 '17 at 18:15
then, your title contradicts with your description. Update your question
â RomanPerekhrest
Dec 13 '17 at 18:16
1
I can't see what the contradiction is? They want to split the line, pick one of the fields and put it in a variable.
â ilkkachu
Dec 13 '17 at 18:21
As a general rule, you really don't want to use the shell for text parsing. It is very hard to do correctly (as you're finding out) and will be very slow. Have a look at Why is using a shell loop to process text considered bad practice? for more details and How can I extract/change lines in a text file whose data are separated into fields? for other options.
â terdonâ¦
Dec 13 '17 at 18:22