LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-20-2013, 03:28 AM   #1
sarnoma
LQ Newbie
 
Registered: Feb 2013
Posts: 3

Rep: Reputation: Disabled
How to access data in .txt with script languages?


Hi, I'm a really noob to script languages and need help from experts.
I wrote a C program written that can take care of 40 digits precise division and I'm supposed to verify if the outputs are correct.

Inputs would be in .txt or .csv file (.txt preferred) and it contains 1000 different inputs.

What I want to do is to write a script in bash so that it takes inputs from .txt file and run the C program for 1000 times and append then to a .txt file.

C code runs as
./foo d1 d2 d3 d4... s1 s2 s3 s4...

and output is
o1 o2 o3 o4...

Input file format is
Code:
d1 d2 d3 d4...
s1 s2 s3 s4...
   .
   .
   .

I'm really new to bash so I don't even know if it is possible or not.
Can anyone help me with this?
 
Old 02-20-2013, 03:58 AM   #2
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
I think xargs would help. However, I wonder why you don't just do this within the C program.
 
Old 02-20-2013, 04:08 AM   #3
sarnoma
LQ Newbie
 
Registered: Feb 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by H_TeXMeX_H View Post
I think xargs would help. However, I wonder why you don't just do this within the C program.
Oh, because I have other codes that do the same thing but with different ways.
I figured that written script code would be helpful on other codes without modifying them.

How would I use xargs?
 
Old 02-20-2013, 04:24 AM   #4
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
You just pipe the list to xargs and it will execute a command with the list as arguments.

Something like:

Code:
cat file.txt | xargs ./program
OR
xargs ./program < file.txt
You can see the man page for more options.
 
1 members found this post helpful.
Old 02-20-2013, 09:21 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
This is a common use case for scripting. You have a specialized program that is a binary executable, which does one thing. You want to use it iteratively to do the same thing over many data sets. So the part of the problem that you need to identify is how to iterate over your data sets. In your case, it looks like the data is in one file, with one operand per line, alternating between operands. So...
Code:
while read operand1 operand2; do
    ./foo "$operand1" $operand2"
done < yourDatafile.txt
This will read two lines on each loop, and provide them as commandline args to your program, as specified.


--- rod.

Last edited by theNbomr; 02-20-2013 at 09:23 AM.
 
1 members found this post helpful.
Old 02-20-2013, 11:17 AM   #6
sarnoma
LQ Newbie
 
Registered: Feb 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by theNbomr View Post
This is a common use case for scripting. You have a specialized program that is a binary executable, which does one thing. You want to use it iteratively to do the same thing over many data sets. So the part of the problem that you need to identify is how to iterate over your data sets. In your case, it looks like the data is in one file, with one operand per line, alternating between operands. So...
Code:
while read operand1 operand2; do
    ./foo "$operand1" $operand2"
done < yourDatafile.txt
This will read two lines on each loop, and provide them as commandline args to your program, as specified.


--- rod.
Wow! That is exactly what I wanted!
Thank you so much!
Works perfectly.
 
Old 02-21-2013, 06:58 AM   #7
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Just to be complete you can use 'xargs -L2' to accomplish the same thing.

Code:
bash-4.2$ printf "d1 d2 d3 d4\ns1 s2 s3 s4\nd1 d2 d3 d4\ns1 s2 s3 s4\n" | xargs -L2 echo
d1 d2 d3 d4 s1 s2 s3 s4
d1 d2 d3 d4 s1 s2 s3 s4
 
1 members found this post helpful.
  


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
[SOLVED] awk question - read in txt files, offset data by given amount, output new txt files pomico Programming 19 09-17-2012 12:43 PM
C - how to open a txt file and then display the data UltimateDesi Programming 20 10-17-2010 11:32 AM
Script for pulling data out of a txt file schapman43 Linux - Newbie 7 08-03-2010 02:07 PM
How can I use Shell script to edit a data at a particular location in a txt file? leena_d Programming 30 02-08-2010 01:43 AM
Modify Perl script to work with txt - Permissions script joangopan Programming 4 09-14-2007 10:20 PM


All times are GMT -5. The time now is 12:27 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration