Parsing string using specific delimiter

Hi,

I'm wondering what is the best way to parse out a long string that has a specific deliminator and outputting each token between the delim on a newline?

i.e. input
text1,text2,text3,tex4

i.e. output
text1
text2
text3
text4

That's not really parsing, that's simple substitution.

tr , '\n' <input >output

If your tr doesn't grok '\n', try '\012' or a literal newline between single quotes. (Looks weird, but is valid and useful syntax in Bourne shell.)

Oh I can't believe I forgot about tr.

Although, this works, I can only get it to recognize a newline but can't combine with that a newline+few tabs?

I also have a string that in the form of:

variable = "data value"

I have an extended sed to remove all instances of the double quotes, is there a on liner that'll grab the right hand side of the "equal" sign and only output the datavalue without the quotes.

Thanks for your help.

You can use cut / awk for this..

a='variable="data value"'

echo $a | cut -d'=' -f2 | sed 's/\"//g'

echo $a | awk -F= '{print $2}' | sed 's/\"//g'

This removes everything up to the first equals sign on every line:

sed -e 's/^[^=]*=//' file

This replaces a comma with a newline and two tabs. Again, there are sed variants which recognize \n and \t and others which don't (probably most don't understand \t) so consult your local sed manual page and/or experiment.

sed -e 's/,/\n\t\t/g' file

This is in response to the original question, you said to use "tr" to add newline but I can't seem to get "tabs" to also be added?

So I get an output with a single tab, and the rest are not tabbed.

Script:

#!/bin/sh

A='text1,text2,text3'

echo -e "Output: \n\t${A}" | tr , '\n'

Script Output:

Output:
        text1
text2
text3

Desired Output:

Output:
        text1
        text2
        text3

tr can't replace one character with many; see the proposed sed solution above instead.

#!/bin/sh

A='text1,text2,text3'

echo $A | sed -e 's/,/\n\t\t/g'

Produces the following:

text1
text2
text3

Still have the offset of the tab, but now it's not the trailing words but the first

UPDATE:

I guess you could do the following to get that first word to get tabbed properly:

echo -e "\t\t$A" | sed -e 's/,/\n\t\t/g'

Or you could add 's/^/\t\t/;' to the beginning of the sed script. The original script replaces commas with the substitution string, and doesn't touch the places where there is not a comma (such as, for example, beginning of line).