splitting a line into array in bash with tab as delimiter

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite
1












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.







share|improve this question






















  • 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














up vote
1
down vote

favorite
1












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.







share|improve this question






















  • 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












up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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.







share|improve this question














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.









share|improve this question













share|improve this question




share|improve this question








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
















  • 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










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





share|improve this answer






















  • can you tell what -a tmp is doing??
    – user3138373
    Dec 13 '17 at 18:20






  • 2




    @user3138373 the -a flag tells read to store what it reads as an array.
    – terdon♦
    Dec 13 '17 at 18:23










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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






























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





share|improve this answer






















  • can you tell what -a tmp is doing??
    – user3138373
    Dec 13 '17 at 18:20






  • 2




    @user3138373 the -a flag tells read to store what it reads as an array.
    – terdon♦
    Dec 13 '17 at 18:23














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





share|improve this answer






















  • can you tell what -a tmp is doing??
    – user3138373
    Dec 13 '17 at 18:20






  • 2




    @user3138373 the -a flag tells read to store what it reads as an array.
    – terdon♦
    Dec 13 '17 at 18:23












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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








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 tells read 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






  • 2




    @user3138373 the -a flag tells read 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












 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?