1

How can I read in POSIX bash input like this:

<name>,<tag1> <tag2> <tag3>…

I tried while read line;do done but this wants newlines, all I have is spaces.

(Is IFS solution? If yes, how? (I don't fully understand IFS.))

5
  • Are these going to be four fields or two fields ?
    – heemayl
    May 17, 2015 at 18:37
  • I don't know what do you mean. Who?
    – TGar
    May 17, 2015 at 18:40
  • If it wants newlines where you have spaces, translate spaces to newlines. (tr ' ' '\n') May 17, 2015 at 18:42
  • @don_crissti I want to work with each of tags alone
    – TGar
    May 17, 2015 at 18:48
  • Are you looking for something like this: echo '<name>,<tag1> <tag2> <tag3>' | tr ", " "\n"?
    – Cyrus
    May 17, 2015 at 18:54

1 Answer 1

3

Use an array:

echo '<name>,<tag1> <tag2> <tag3>' | while IFS=" ," read -a foo; do echo ${foo[@]}; done

Output:

<name> <tag1> <tag2> <tag3>

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .