LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-09-2014, 05:45 AM   #1
tripialos
Member
 
Registered: Apr 2012
Posts: 163

Rep: Reputation: Disabled
loading args from file with multiple lines


Greetings

Happy new year

I have a file "myfile.txt" which has the following content and this content is used as arguments for my script:

10 1203
88 1010
76 765
1 122

I made a script which performs some calculations and i run it as follows:

myscript.sh 10 1203


How can i load each line from the file to the script. In other words, i want the scrip to perform the calculations for each line in the file.


Thanks

Last edited by tripialos; 01-09-2014 at 05:53 AM.
 
Old 01-09-2014, 05:56 AM   #2
lpwevers
Member
 
Registered: Apr 2005
Location: The Netherlands
Distribution: SuSE, CentOS
Posts: 128

Rep: Reputation: 20
Hi,

Something like this should do the trick I guess:
Code:
cat myfile.txt | while read F
do
  myscript.sh $F
done
 
Old 01-09-2014, 06:05 AM   #3
tripialos
Member
 
Registered: Apr 2012
Posts: 163

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by lpwevers View Post
Hi,

Something like this should do the trick I guess:
Code:
cat myfile.txt | while read F
do
  myscript.sh $F
done
Thanks for the replay!

Yes id does the trick :-D

Is it possible to embed this functionality within the script myscript.sh

For example:
Code:
While reading lines from myfile.txt
load the line as arguments and run the calculations
once lines from file finished
exit
 
Old 01-09-2014, 06:15 AM   #4
lpwevers
Member
 
Registered: Apr 2005
Location: The Netherlands
Distribution: SuSE, CentOS
Posts: 128

Rep: Reputation: 20
Quote:
Originally Posted by tripialos View Post
Thanks for the replay!

Yes id does the trick :-D

Is it possible to embed this functionality within the script myscript.sh

For example:
Code:
While reading lines from myfile.txt
load the line as arguments and run the calculations
once lines from file finished
exit
Hi,

Glad it worked. You can embed the loop in your script:
Code:
# myscript.sh
ARGFILE=$1

cat $ARGFILE | while read F
do
  ARG1=`echo $F | awk '{ print $1 }'`
  ARG2=`echo $F | awk '{ print $2 }'`
  ...
  <command> $ARG1 $ARG2
  <blah> $ARG1
  ...
done
Now you pass the name of the file with the argments on the command line so your call will be something like this:
Code:
./myscript.sh myfile.txt
Then, within the script, the values are stored in $ARG1 and $ARG2 so you can use them in your calculations.

Louis
 
Old 01-09-2014, 06:25 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
@tripialos: If there is just one myfile.txt (or alike) you can simplify the above:
Code:
#!/bin/bash

while read FIRST_ITEM SECOND_ITEM
do
  echo "First item is  : $FIRST_ITEM"
  echo "Second item is : $SECOND_ITEM"
  # do something with the current line of input here
done < myfile.txt
The above script can be run as ./scriptname

If you have to be able to use different input files you can do this:
Code:
#!/bin/bash

while read FIRST_ITEM SECOND_ITEM
do
  echo "First item is  : $FIRST_ITEM"
  echo "Second item is : $SECOND_ITEM"
  # do something with the current line of input here
done < $1
The above can be run as ./scriptname input.file
 
Old 01-09-2014, 07:56 AM   #6
tripialos
Member
 
Registered: Apr 2012
Posts: 163

Original Poster
Rep: Reputation: Disabled
sorted with both solutions!!!!

Thanks guys!!
 
  


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] [Perl] Reading multiple lines from a file Ziddy Programming 3 10-30-2012 08:27 AM
[SOLVED] How to delete multiple lines in a file using perl yjy4321 Programming 14 04-21-2011 11:15 AM
Deleting multiple lines above and below an expression in a file Ransak Programming 7 05-20-2010 09:08 PM
Automated editing of multiple lines in a file Curtor Linux - Newbie 3 10-23-2008 05:24 PM
bash: read multiple lines in a file 300zxkyle Programming 7 07-29-2007 05:38 AM


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