ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I might be overlooking something really obvious here but...
I have a single string of text, with numerous commas in it. I want to parse the string, and use the comma as a delimeter to separate it into multiple strings hopefully just in shell scripting.
You use the -F flag in awk to tell it what to use for field separator (comma in this case).
The print statement with commas in it causes the output to have spaces instead of commas (odd isn't it?)
The set command populates the positional parameters ($1 through $n).
You then initialize arbitrary variable names (I chose VAL1 through VAL4 - you could choose bob, judy, ralph and david or whatever) by assigning each of the positional parameters to one of the arbitrary ones.
Lastly you simply echo each of the arbitrary variables which will show the value it now contains.
if you're within a bash, you can split the input using arrays:
Code:
jan@jack:~> IFS=',
> '
jan@jack:~> echo "value1,value2,value3,value4" | while read -a inp; do
> for member in ${inp[@]}; do
> echo $member
> done
> done
value1
value2
value3
value4
IFS defines the Input Field Separator, read -a reads all values of a line into an array. The for loop iterates over all elements of the array. It works also for multiple line input:
Code:
jan@jack:~> echo -e "value1,value2,value3,value4\nv5,v6\nval7,val8,val9" |
> while read -a inp; do
> echo "new line"
> for member in ${inp[@]}; do
> echo $member
> done
> done
new line
value1
value2
value3
value4
new line
v5
v6
new line
val7
val8
val9
Sorry should have explained this better... those options are all fine if I know how many variables there are. however, I want to do this on a string of an undetermined length.
One time it could be 1 string to 10 strings or 1 string to 500 strings or more.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.