LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-29-2009, 04:48 AM   #1
m4rtin
Member
 
Registered: Sep 2007
Posts: 261

Rep: Reputation: 16
bash script stdin accept values separated with new lines, commas, spaces


I have a script, where STDIN(numbers from a text file) is copied from a text file using mouse and pasted to the terminal window.

Code:
user@computer:~>./script -o -t <PASTE NUMBERS GOES HERE>
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?
 
Old 12-29-2009, 06:23 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
tell your user to put quotes when they key in the argument
Code:
./script -t -o "paste here"
or inside your script, save $@ to variable
 
Old 12-29-2009, 06:36 AM   #3
m4rtin
Member
 
Registered: Sep 2007
Posts: 261

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by ghostdog74 View Post
tell your user to put quotes when they key in the argument
Code:
./script -t -o "paste here"
or inside your script, save $@ to variable
could you explain this $@ variable option?
 
Old 12-29-2009, 07:31 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
$@ contains all your arguments. Read the bash manual.
 
Old 12-30-2009, 05:19 AM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by ghostdog74 View Post
$@ 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
 
Old 12-30-2009, 05:26 AM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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

Last edited by ta0kira; 12-30-2009 at 05:34 AM.
 
Old 12-30-2009, 06:22 AM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
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[@]>
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
redirecting stdin in bash script artur Programming 3 12-09-2011 06:07 AM
Elements in a "for loop" using bash not being separated by white spaces. ralste Programming 6 11-10-2009 02:26 AM
how to clear file form spaces and add commas ? czezz Programming 11 06-18-2009 06:57 AM
Parsing a comma separated CSV file where fields have commas in to trickyflash Linux - General 7 03-26-2009 03:30 PM
help with comma separated values and what should be a simple script. zaber Programming 10 03-06-2008 12:58 PM


All times are GMT -5. The time now is 01:27 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration