LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 12-01-2011, 06:55 PM   #1
kesi
LQ Newbie
 
Registered: Dec 2011
Posts: 21

Rep: Reputation: Disabled
Help reading numbers from file with bash.


I'm trying to take numbers from a large text file that consists of a number on each line and then using them in curl to retrieve some files.


What I've been doing is:

FILE=filename.txt
for line in $(cut -d: -f1 $FILE)
do
curl 'http://mydomain.com/pet.php?num={$line}&date=2011 -o fileout.xml
done

The curl line works if I just copy all of the numbers in, comma-delimited, but in this format doesn't seem to grab the info.

Also, if I replace curl with echo, it prints all of the numbers to the screen.
Suggestions? Thanks!
 
Old 12-01-2011, 07:21 PM   #2
jb_gpk
Member
 
Registered: Dec 2010
Distribution: Debian
Posts: 30

Rep: Reputation: 13
it seems to work fine here...

what result do you got? any error message?

the script that you are executing have this singe quote before http? ('http) :P
 
Old 12-01-2011, 07:23 PM   #3
kesi
LQ Newbie
 
Registered: Dec 2011
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jb_gpk View Post
it seems to work fine here...

what result do you got? any error message?

the script that you are executing have this singe quote before http? ('http) :P
That was a typo. It has a single quote before and after the link. It doesn't seem to be inserting the info because it doesn't pull the page and just overwrites the same file over and over. If I insert the number manually, it works.
 
Old 12-01-2011, 07:27 PM   #4
kesi
LQ Newbie
 
Registered: Dec 2011
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by kesi View Post
That was a typo. It has a single quote before and after the link. It doesn't seem to be inserting the info because it doesn't pull the page and just overwrites the same file over and over. If I insert the number manually, it works.
Basically, there's a default page supplied when no id number is provided and that's the one I get. Also, if I change the output to 2011_#1.xml and have the numbers manually entered, it will name each file like such number=1234 file=2011_1234.xml. In this case, it's named 2011_$line.xml
 
Old 12-01-2011, 07:39 PM   #5
kesi
LQ Newbie
 
Registered: Dec 2011
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jb_gpk View Post
it seems to work fine here...

what result do you got? any error message?

the script that you are executing have this singe quote before http? ('http) :P
Aha! It does seem to be the quotes causing the variable to not be read. I had them there because some of the link have ampersands but it's easier to just escape them.

Thanks!
 
Old 12-01-2011, 07:43 PM   #6
jb_gpk
Member
 
Registered: Dec 2010
Distribution: Debian
Posts: 30

Rep: Reputation: 13
when you put quotes what is inside them is treated as a "string"
 
Old 12-01-2011, 07:45 PM   #7
kesi
LQ Newbie
 
Registered: Dec 2011
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jb_gpk View Post
when you put quotes what is inside them is treated as a "string"
Yep. Thanks.
 
Old 12-01-2011, 07:50 PM   #8
Cameron04
LQ Newbie
 
Registered: Nov 2011
Posts: 1

Rep: Reputation: Disabled
Hi, i am a little comfused about what you are talking about.
 
Old 12-01-2011, 07:55 PM   #9
kesi
LQ Newbie
 
Registered: Dec 2011
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Cameron04 View Post
Hi, i am a little comfused about what you are talking about.
What are you confused about?
 
Old 12-01-2011, 09:00 PM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability.

It usually helps to an example of the input text as well, when diagnosing problems.


Also, you shouldn't read lines with a for loop. A while+read loop is safer and more efficient. It also lets you avoid using cut.

Code:
FILE=filename.txt

while IFS=':' read -r line trash; do
	curl "http://mydomain.com/pet.php?num={$line}&date=2011" -o fileout.xml
done <"$FILE"
The trash variable will catch everything in the line after the first column.

Also, using double-quotes around the string escapes most characters, but still treats "$" as special, allowing variables to expand. Single quotes escape everything.

See the quoting section of the bash man page, and here:
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes
 
  


Reply



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
Bash script to compare numbers in a txt file leopard86 Programming 6 09-11-2012 12:10 AM
how to add numbers in a formatted file via a bash script? zero79 Linux - General 8 12-24-2010 05:48 PM
Bash + Reading values(numbers) from a file and storing them into an array SuchANewb Linux - Newbie 5 11-04-2010 08:11 AM
[SOLVED] Reading numbers from text file and storing in array idaham Linux - General 3 05-27-2010 03:36 AM
Shell script:- Reading numbers embedded in brackets from a text file rsan Linux - Newbie 6 07-05-2009 06:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:48 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