LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 02-12-2014, 10:58 PM   #1
tuxtuxtux
LQ Newbie
 
Registered: Dec 2008
Posts: 11

Rep: Reputation: Disabled
C Program in Bash :Passing Newline in Command Line Arguments


Hi,
I have a C program under Ubuntu linux that processes data from a text file.The text file actually contains float values seperated by newlines.The program reads this text file on startup and processes the numbers in it.

However it would be great if i could just pass the actual numbers in the text file as arguments to the program directly.

These numbers are values from a website which are seperated by newline.Is there a way that i can select this numbers directly from the website and then paste them as arguments to my C program.

I know of tr ,but can it be used for this task in any way.By using 'tr' and 'here' document i was able to change the newline to space.But i cannot give the output to my c program.

Can anybody help me on this.
thanks in advance
tux
 
Old 02-12-2014, 11:22 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
How are you trying to 'give' the output to your program? Does your program accept data from other sources other than a file?
 
Old 02-12-2014, 11:27 PM   #3
tuxtuxtux
LQ Newbie
 
Registered: Dec 2008
Posts: 11

Original Poster
Rep: Reputation: Disabled
yes,i can modify the program for that.What it currently does is ,it reads numbers from file and pass them for processing.But instead if i can give numbers seperated by newline itself in command line,i could remove the file reading portion and process the numbers directly.At that time i couldnt think of anyway other than to store the values in a file and read it from there.Thats why i introduced the file reading part.It is better if i can avoid that intermediate step.
 
Old 02-12-2014, 11:56 PM   #4
tuxtuxtux
LQ Newbie
 
Registered: Dec 2008
Posts: 11

Original Poster
Rep: Reputation: Disabled
Hello I think i have found a solution.It is as follows.I have to include the tr portion in back quotes.Then after the 'here document' start, i paste the numbers from the web.After that i have to give % and then end the back quote.Then i am calling my program with the variable 'a'.It works.

a=`tr '\n' ' '<<%
1516.4
1514.85
1515.15
1515
1515
1515.6
1515.25
1515.5
1514.05
%`;./prog.o $a

Is there any other easy way for this?In this case i have to paste the text and then type the remaining piece of command.
 
Old 02-13-2014, 01:01 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am a little confused at the need for tr? If your program is able to read from a file then it can already handle the newlines itself (yes?).

Your options are more around how to get the data from the website to your application.

Can your program read from stdin? If yes, then simply redirect the data to your application.
First method would be a simple:
Code:
$ echo '1516.4
1514.85
1515.15
1515
1515
1515.6
1515.25
1515.5
1514.05' | ./prog.o
Once you have this working you can find an alternate way to retrieve the data from the website, maybe wget or Perl or some such ... maybe even from within your application which you pass the web address

Ultimately the answer will rely on how much work you wish to do and perhaps consideration of whether other users may use it or in fact how often you may use it?
 
Old 02-13-2014, 03:29 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
I don't really get the question, but here is an example for something similar:

Code:
ARGS=$'lines\nseparated by\nnewlines'; myecho -d -- $ARGS
myecho -debug: 4 arguments:
   1: 'lines'
   2: 'separated'
   3: 'by'
   4: 'newlines'
(myecho is a trivial demo program to echo its parameters)
 
Old 02-13-2014, 06:23 AM   #7
tuxtuxtux
LQ Newbie
 
Registered: Dec 2008
Posts: 11

Original Poster
Rep: Reputation: Disabled
I am also little confused about why the question is confusing.
Normally when we run a "c program with command line arguments" from command line in bash,we do as follows


bashprompt$./prog.o 1 2 3 4

But my queston is "what happens when the 1 2 3 4 are seperated by newline" like following

bashprompt$./prog.o 1
2
3
4

Here the program will only take 1 and the bash prompt will report error for other args as "unknown command."

The website from where i take data contain these numbers in a column seperated by newline.When I copy and paste it to bash prompt,it will take only the first value.That was the issue.I have only found a workaround using 'tr' as mentioned in my previous post.I hope there are more elegant solutions to it,as I have heard that Bash can pass arguments like this to programs.
{You can try copying the above post itself and pasting after an echo in the bash prompt to see what my problem is!!!!}

Thankyou very much for the suggestions.
 
Old 02-13-2014, 06:29 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Then adapt my previous post:

Code:
$ ARG='
<paste the data here -- mouse-middle-button / Shift+Insert>
'; myecho -d -- $ARG
myecho -debug: 4 arguments:
   1: '1'
   2: '2'
   3: '3'
   4: '4'
Or:
Code:
$ myecho -d -- $(cat)
<paste the data here -- mouse-middle-button / Shift+Insert>
<Ctrl+D>
myecho -debug: 4 arguments:
   1: '1'
   2: '2'
   3: '3'
   4: '4'
Or:
Code:
$ xargs myecho -d --
<paste the data here -- mouse-middle-button / Shift+Insert>
<Ctrl+D>
myecho -debug: 4 arguments:
   1: '1'
   2: '2'
   3: '3'
   4: '4'

Last edited by NevemTeve; 02-13-2014 at 06:34 AM.
 
Old 02-13-2014, 06:34 AM   #9
tuxtuxtux
LQ Newbie
 
Registered: Dec 2008
Posts: 11

Original Poster
Rep: Reputation: Disabled
To clarify again,what I want to do is to convert the newlines in the arguments to "spaces" before passing it to my program.I hope that clears it.

Thanks NevemTeve for the suggestion.I am not currently at my linux machine to test it.Do you think it will work as I expect?
 
Old 02-13-2014, 06:36 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
I don't know what do you really want, but I did test everything I quoted into this topic.
 
Old 02-13-2014, 06:44 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
what I want to do is to convert the newlines in the arguments to "spaces" before passing it to my program
And you are more than welcome to do so, but what NevemTeve and i have tried to tell you is that the C program can be made (or already does know how) to deal with newlines, the only diffrence
is how you pass it to your program.

Your example is inaccurate because prior to pasting, if you wish to pass new line separated data:
Code:
1
2
3
4
to your program, you must, like you would pass it to any other program or script, quote the data to maintain the newlines.
So your example should look like:
Code:
bashprompt$ ./prog.o '1
2
3
4'
Now the program will see this as a contiguous string which happens to be separated by new lines.
 
Old 02-13-2014, 07:00 AM   #12
tuxtuxtux
LQ Newbie
 
Registered: Dec 2008
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thanks.It is very simple, it seems.I may have missed that method first when i tried with my program.I am not sure now.I have to test it later in my linux box and will comment.
Well hoping that it will work I want to comment that "This is exactly why i posted the question here.I know that there was some simple solution and I was missing it".
 
Old 02-13-2014, 01:25 PM   #13
tuxtuxtux
LQ Newbie
 
Registered: Dec 2008
Posts: 11

Original Poster
Rep: Reputation: Disabled
Hi NevemTeve,

The suggestion of you in post 8 using xargs worked well.(I havent tested the others).Thankyou verymuch. However the '--' after -d also gets passed as an argument to the program.But when I removed the -- everything worked as expected.Thankyou again.

<code>
$ xargs myecho -d --
<paste the data here -- mouse-middle-button / Shift+Insert>
<Ctrl+D>
myecho -debug: 4 arguments:
1: '1'
2: '2'
3: '3'
4: '4'
</code>

grail,
the passing of args by '' didnt go as I expected in the program.In that case the program gets all the arguments, but they still contain newlines.SO the program needs to process and remove them.I needed something that replaces newlines with spaces before passing to the program.The solution using xargs mentioned above worked fine.
 
Old 02-14-2014, 03:18 AM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
True, the whole myecho -d -- part has to be replaced with your own program.

(Note: the usual 'echo' isn't very useful when testing command-line arguments, eg echo a b c and echo 'a b c' give the same output.
 
  


Reply

Tags
bash, code, comand, newline, tr



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
passing an array as command line arguments? etika Linux - Newbie 5 04-03-2011 12:40 PM
Passing command line arguments jason_m Programming 4 08-26-2010 10:36 PM
Passing command-line arguments to qglviewer application MALDATA Programming 1 07-15-2009 09:27 AM
Passing command line arguments through getopts and long options neville310 Programming 3 04-16-2007 06:38 AM
Passing a text file to the command line as arguments wimnat Linux - General 2 12-05-2005 08:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:29 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration