bash script stdin accept values separated with new lines, commas, spaces
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 mean the user should type ./script -o -t , then open this text file and copy particular numbers and paste those into the terminal window right after the ./script -o -t . The point is, that those numbers may be separated with space, with commas or with new lines:
Quote:
TEXT FILE EXAMPLE1:
----------
85 72 33423 2389 2312310
----------
TEXT FILE EXAMPLE2:
----------
23748923, 234723, 2, 0923, 1
----------
TEXT FILE EXAMPLE3:
----------
324
4
5
1231
18
----------
Is it possible to have a script, which is able to take input weather it's separated using commas, new lines or spaces?
$@ contains all your arguments. Read the bash manual.
Your suggestion to quote was a better one. This will fail if a double-space has similar meaning to ,, (i.e. an empty field.) My suggestion is to use standard input and not have to worry about all of this.
Kevin Barry
Now that I mention it, your question is rather ambiguous. Does the user press [Enter] before pasting? It doesn't look like it, but what you've described isn't standard input; it's command-line arguments. Can you post a basic example? Of a script, input, and what's expected to happen.
Regarding the question you seem to be asking, I'd pipe the input through tr -s ', ' '\n' to put each group on its own line.
Kevin Barry
You can make a script accept an argument and split its contents using arrays and the read builtin.
For example:
Code:
#!/bin/bash
IFS=$' ,\t\n'
{
set -- $1
NUMBERS=("$@")
}
# or
{
while read -a TEMP; do
NUMBERS=("${NUMBERS[@}}" "${TEMP[@]}")
done
} <<< "$1"
# ---
<do something with NUMBERS[@]>
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.