IFS splitting issue












3















I am using the following line at the beginning of a bash shell script:



IFS=':#:'


But it is not separating fields with :#:, only with colon. What is the issue?



EDIT:



This is my data in text file:



f:#:0
c:#:Test C
s:#:test S
ctype:#:0
a:#:test A
t:#:10:02:03
r:#:test r

f:#:0
c:#:Test C1
s:#:test S1
ctype:#:1
a:#:test A1
t:#:00:02:22
r:#:test r

f:#:20
c:#:Test C
s:#:test S
ctype:#:2
a:#:test A1
t:#:00:02:03
r:#:test r


... and I am reading it using the following code:



IFS=':#:'   
while read -r key value; do
.....
done < "$FileName"









share|improve this question

























  • Note that in this case you don't really need a multicharacter separator - you can treat the # as a field and simply discard it e.g. while IFS=: read -r key _ value

    – steeldriver
    Dec 28 '16 at 13:00
















3















I am using the following line at the beginning of a bash shell script:



IFS=':#:'


But it is not separating fields with :#:, only with colon. What is the issue?



EDIT:



This is my data in text file:



f:#:0
c:#:Test C
s:#:test S
ctype:#:0
a:#:test A
t:#:10:02:03
r:#:test r

f:#:0
c:#:Test C1
s:#:test S1
ctype:#:1
a:#:test A1
t:#:00:02:22
r:#:test r

f:#:20
c:#:Test C
s:#:test S
ctype:#:2
a:#:test A1
t:#:00:02:03
r:#:test r


... and I am reading it using the following code:



IFS=':#:'   
while read -r key value; do
.....
done < "$FileName"









share|improve this question

























  • Note that in this case you don't really need a multicharacter separator - you can treat the # as a field and simply discard it e.g. while IFS=: read -r key _ value

    – steeldriver
    Dec 28 '16 at 13:00














3












3








3








I am using the following line at the beginning of a bash shell script:



IFS=':#:'


But it is not separating fields with :#:, only with colon. What is the issue?



EDIT:



This is my data in text file:



f:#:0
c:#:Test C
s:#:test S
ctype:#:0
a:#:test A
t:#:10:02:03
r:#:test r

f:#:0
c:#:Test C1
s:#:test S1
ctype:#:1
a:#:test A1
t:#:00:02:22
r:#:test r

f:#:20
c:#:Test C
s:#:test S
ctype:#:2
a:#:test A1
t:#:00:02:03
r:#:test r


... and I am reading it using the following code:



IFS=':#:'   
while read -r key value; do
.....
done < "$FileName"









share|improve this question
















I am using the following line at the beginning of a bash shell script:



IFS=':#:'


But it is not separating fields with :#:, only with colon. What is the issue?



EDIT:



This is my data in text file:



f:#:0
c:#:Test C
s:#:test S
ctype:#:0
a:#:test A
t:#:10:02:03
r:#:test r

f:#:0
c:#:Test C1
s:#:test S1
ctype:#:1
a:#:test A1
t:#:00:02:22
r:#:test r

f:#:20
c:#:Test C
s:#:test S
ctype:#:2
a:#:test A1
t:#:00:02:03
r:#:test r


... and I am reading it using the following code:



IFS=':#:'   
while read -r key value; do
.....
done < "$FileName"






shell-script text-processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 22 mins ago









Jeff Schaller

41.5k1056132




41.5k1056132










asked Dec 28 '16 at 5:04









Bhumi ShahBhumi Shah

1337




1337













  • Note that in this case you don't really need a multicharacter separator - you can treat the # as a field and simply discard it e.g. while IFS=: read -r key _ value

    – steeldriver
    Dec 28 '16 at 13:00



















  • Note that in this case you don't really need a multicharacter separator - you can treat the # as a field and simply discard it e.g. while IFS=: read -r key _ value

    – steeldriver
    Dec 28 '16 at 13:00

















Note that in this case you don't really need a multicharacter separator - you can treat the # as a field and simply discard it e.g. while IFS=: read -r key _ value

– steeldriver
Dec 28 '16 at 13:00





Note that in this case you don't really need a multicharacter separator - you can treat the # as a field and simply discard it e.g. while IFS=: read -r key _ value

– steeldriver
Dec 28 '16 at 13:00










2 Answers
2






active

oldest

votes


















2














As pointed out by @heemayl the problem is that IFS doesn't treat the whole string as the separator, it treats each char as a individual separator. awk however is able to use a string as a delim.



For example:



#!/bin/bash
while read -r key value
do
printf 'key %-7s val %sn' "$key" "$value"
done < <(awk -F ':#:' '{print $1" "$2}' $FileName )

key f val 0
key c val Test C
key s val test S
key ctype val 0
key a val test A
key t val 10:02:03
key r val test r
key val
key f val 0
key c val Test C1
key s val test S1
key ctype val 1
key a val test A1
key t val 00:02:22
key r val test r
key val
key f val 20
key c val Test C
key s val test S
key ctype val 2
key a val test A1
key t val 00:02:03
key r val test r





share|improve this answer


























  • What if i am using this in while loop as seperator and reading mulitple lines from files with this seperator?

    – Bhumi Shah
    Dec 28 '16 at 5:29











  • If you're looking for a comprehensive answer we are going to need more information to go on. Post a code sample of what you have with some example input and expected output.

    – Zachary Brady
    Dec 28 '16 at 5:32











  • edited in question..see now

    – Bhumi Shah
    Dec 28 '16 at 5:41











  • @BhumiShah updated my answer

    – Zachary Brady
    Dec 28 '16 at 5:59











  • Not working. getting error Syntax error: redirection unexpected

    – Bhumi Shah
    Dec 28 '16 at 6:09



















2














IFS does not use multiple characters (or a range) as separator; each character in IFS is treated as a field separator.



From man bash:




IFS The Internal Field Separator that is used for word splitting
after expansion and to split lines into words with the read builtin
command. The default value is
<space><tab><newline>.







share|improve this answer
























  • Is there any way for multiple characters separator?

    – Bhumi Shah
    Dec 28 '16 at 5:10











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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
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%2f333190%2fifs-splitting-issue%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














As pointed out by @heemayl the problem is that IFS doesn't treat the whole string as the separator, it treats each char as a individual separator. awk however is able to use a string as a delim.



For example:



#!/bin/bash
while read -r key value
do
printf 'key %-7s val %sn' "$key" "$value"
done < <(awk -F ':#:' '{print $1" "$2}' $FileName )

key f val 0
key c val Test C
key s val test S
key ctype val 0
key a val test A
key t val 10:02:03
key r val test r
key val
key f val 0
key c val Test C1
key s val test S1
key ctype val 1
key a val test A1
key t val 00:02:22
key r val test r
key val
key f val 20
key c val Test C
key s val test S
key ctype val 2
key a val test A1
key t val 00:02:03
key r val test r





share|improve this answer


























  • What if i am using this in while loop as seperator and reading mulitple lines from files with this seperator?

    – Bhumi Shah
    Dec 28 '16 at 5:29











  • If you're looking for a comprehensive answer we are going to need more information to go on. Post a code sample of what you have with some example input and expected output.

    – Zachary Brady
    Dec 28 '16 at 5:32











  • edited in question..see now

    – Bhumi Shah
    Dec 28 '16 at 5:41











  • @BhumiShah updated my answer

    – Zachary Brady
    Dec 28 '16 at 5:59











  • Not working. getting error Syntax error: redirection unexpected

    – Bhumi Shah
    Dec 28 '16 at 6:09
















2














As pointed out by @heemayl the problem is that IFS doesn't treat the whole string as the separator, it treats each char as a individual separator. awk however is able to use a string as a delim.



For example:



#!/bin/bash
while read -r key value
do
printf 'key %-7s val %sn' "$key" "$value"
done < <(awk -F ':#:' '{print $1" "$2}' $FileName )

key f val 0
key c val Test C
key s val test S
key ctype val 0
key a val test A
key t val 10:02:03
key r val test r
key val
key f val 0
key c val Test C1
key s val test S1
key ctype val 1
key a val test A1
key t val 00:02:22
key r val test r
key val
key f val 20
key c val Test C
key s val test S
key ctype val 2
key a val test A1
key t val 00:02:03
key r val test r





share|improve this answer


























  • What if i am using this in while loop as seperator and reading mulitple lines from files with this seperator?

    – Bhumi Shah
    Dec 28 '16 at 5:29











  • If you're looking for a comprehensive answer we are going to need more information to go on. Post a code sample of what you have with some example input and expected output.

    – Zachary Brady
    Dec 28 '16 at 5:32











  • edited in question..see now

    – Bhumi Shah
    Dec 28 '16 at 5:41











  • @BhumiShah updated my answer

    – Zachary Brady
    Dec 28 '16 at 5:59











  • Not working. getting error Syntax error: redirection unexpected

    – Bhumi Shah
    Dec 28 '16 at 6:09














2












2








2







As pointed out by @heemayl the problem is that IFS doesn't treat the whole string as the separator, it treats each char as a individual separator. awk however is able to use a string as a delim.



For example:



#!/bin/bash
while read -r key value
do
printf 'key %-7s val %sn' "$key" "$value"
done < <(awk -F ':#:' '{print $1" "$2}' $FileName )

key f val 0
key c val Test C
key s val test S
key ctype val 0
key a val test A
key t val 10:02:03
key r val test r
key val
key f val 0
key c val Test C1
key s val test S1
key ctype val 1
key a val test A1
key t val 00:02:22
key r val test r
key val
key f val 20
key c val Test C
key s val test S
key ctype val 2
key a val test A1
key t val 00:02:03
key r val test r





share|improve this answer















As pointed out by @heemayl the problem is that IFS doesn't treat the whole string as the separator, it treats each char as a individual separator. awk however is able to use a string as a delim.



For example:



#!/bin/bash
while read -r key value
do
printf 'key %-7s val %sn' "$key" "$value"
done < <(awk -F ':#:' '{print $1" "$2}' $FileName )

key f val 0
key c val Test C
key s val test S
key ctype val 0
key a val test A
key t val 10:02:03
key r val test r
key val
key f val 0
key c val Test C1
key s val test S1
key ctype val 1
key a val test A1
key t val 00:02:22
key r val test r
key val
key f val 20
key c val Test C
key s val test S
key ctype val 2
key a val test A1
key t val 00:02:03
key r val test r






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 28 '16 at 6:15

























answered Dec 28 '16 at 5:18









Zachary BradyZachary Brady

3,416932




3,416932













  • What if i am using this in while loop as seperator and reading mulitple lines from files with this seperator?

    – Bhumi Shah
    Dec 28 '16 at 5:29











  • If you're looking for a comprehensive answer we are going to need more information to go on. Post a code sample of what you have with some example input and expected output.

    – Zachary Brady
    Dec 28 '16 at 5:32











  • edited in question..see now

    – Bhumi Shah
    Dec 28 '16 at 5:41











  • @BhumiShah updated my answer

    – Zachary Brady
    Dec 28 '16 at 5:59











  • Not working. getting error Syntax error: redirection unexpected

    – Bhumi Shah
    Dec 28 '16 at 6:09



















  • What if i am using this in while loop as seperator and reading mulitple lines from files with this seperator?

    – Bhumi Shah
    Dec 28 '16 at 5:29











  • If you're looking for a comprehensive answer we are going to need more information to go on. Post a code sample of what you have with some example input and expected output.

    – Zachary Brady
    Dec 28 '16 at 5:32











  • edited in question..see now

    – Bhumi Shah
    Dec 28 '16 at 5:41











  • @BhumiShah updated my answer

    – Zachary Brady
    Dec 28 '16 at 5:59











  • Not working. getting error Syntax error: redirection unexpected

    – Bhumi Shah
    Dec 28 '16 at 6:09

















What if i am using this in while loop as seperator and reading mulitple lines from files with this seperator?

– Bhumi Shah
Dec 28 '16 at 5:29





What if i am using this in while loop as seperator and reading mulitple lines from files with this seperator?

– Bhumi Shah
Dec 28 '16 at 5:29













If you're looking for a comprehensive answer we are going to need more information to go on. Post a code sample of what you have with some example input and expected output.

– Zachary Brady
Dec 28 '16 at 5:32





If you're looking for a comprehensive answer we are going to need more information to go on. Post a code sample of what you have with some example input and expected output.

– Zachary Brady
Dec 28 '16 at 5:32













edited in question..see now

– Bhumi Shah
Dec 28 '16 at 5:41





edited in question..see now

– Bhumi Shah
Dec 28 '16 at 5:41













@BhumiShah updated my answer

– Zachary Brady
Dec 28 '16 at 5:59





@BhumiShah updated my answer

– Zachary Brady
Dec 28 '16 at 5:59













Not working. getting error Syntax error: redirection unexpected

– Bhumi Shah
Dec 28 '16 at 6:09





Not working. getting error Syntax error: redirection unexpected

– Bhumi Shah
Dec 28 '16 at 6:09













2














IFS does not use multiple characters (or a range) as separator; each character in IFS is treated as a field separator.



From man bash:




IFS The Internal Field Separator that is used for word splitting
after expansion and to split lines into words with the read builtin
command. The default value is
<space><tab><newline>.







share|improve this answer
























  • Is there any way for multiple characters separator?

    – Bhumi Shah
    Dec 28 '16 at 5:10
















2














IFS does not use multiple characters (or a range) as separator; each character in IFS is treated as a field separator.



From man bash:




IFS The Internal Field Separator that is used for word splitting
after expansion and to split lines into words with the read builtin
command. The default value is
<space><tab><newline>.







share|improve this answer
























  • Is there any way for multiple characters separator?

    – Bhumi Shah
    Dec 28 '16 at 5:10














2












2








2







IFS does not use multiple characters (or a range) as separator; each character in IFS is treated as a field separator.



From man bash:




IFS The Internal Field Separator that is used for word splitting
after expansion and to split lines into words with the read builtin
command. The default value is
<space><tab><newline>.







share|improve this answer













IFS does not use multiple characters (or a range) as separator; each character in IFS is treated as a field separator.



From man bash:




IFS The Internal Field Separator that is used for word splitting
after expansion and to split lines into words with the read builtin
command. The default value is
<space><tab><newline>.








share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 28 '16 at 5:09









heemaylheemayl

35.4k375105




35.4k375105













  • Is there any way for multiple characters separator?

    – Bhumi Shah
    Dec 28 '16 at 5:10



















  • Is there any way for multiple characters separator?

    – Bhumi Shah
    Dec 28 '16 at 5:10

















Is there any way for multiple characters separator?

– Bhumi Shah
Dec 28 '16 at 5:10





Is there any way for multiple characters separator?

– Bhumi Shah
Dec 28 '16 at 5:10


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f333190%2fifs-splitting-issue%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

宮崎県

濃尾地震

シテ島