importing data from a text file to a bash script
Solution 1
Are you trying to read each line as the symbol?
Have the 25 entries be in a file called stocks.txt. Then use the following script to iterate over each line in the stocks file and process the symbol using the stock function declared in the same script:
while read symbol ; do
stock $symbol >> /home/user/Desktop/stockquote.txt
done < stocks.txt
Solution 2
Apparently, the title and the body of the question did not seem quite the same to me. Here are two solutions:
Importing data from an external file into a Bash script:
Read the file and store its contents as entries in an array. (Note: this is feasible given the contents of your text file. If, for e.g., any line in the file had multiple words without quotations, the array so formed would not be as desired.)
declare -a symbols=($(cat input_file.txt | tr '\n' ' '))
Now, iterate over the items of the array symbols
as follows.
LOG_FILE=/home/user/Desktop/stockquote.txt
for symbol in ${symbols[@]}
do
# echo "Processing $symbol ..."
sh stock "$symbol" >> "$LOG_FILE"
done
In fact, you can embed this in the script named stock itself. Just change the sh stock "$symbol"
above to something like this:
f_process "$symbol" >> "$LOG_FILE"
where f_process
is possibly a function processing the symbol. If you don't want to write a function, just put the relevant lines of code there.
Passing data from an external file as arguments to a shell script:
A related solution has already been posted by neowulf33. Am just reproducing that with corrections:
LOG_FILE=/home/user/Desktop/stockquote.txt
while read symbol
do
stock "$symbol" >> "$LOG_FILE"
done < input_file.txt
Related videos on Youtube
user177073
Updated on September 18, 2022Comments
-
user177073 over 1 year
I need to import a text file like this below:
AMBI CBNK CCXI CERE CLACW CNYD DAEG DLIA DLLR DNDN DSGX HAST HIBB, HPOL IRDMZ MARK NEPT NFEC NINE NRCIB OMER PLCC PLPM PSUN UNTK
There are 25 entries, I need to pass them into a bash script.
I want to run the program once for each symbol. The symbols are Nasdaq stock symbols. I'm trying to pull a quote with the script.
I've been able to do this command manually:
sh stock (symbol) > /home/user/Desktop/stockquote.txt
by inputting the (symbol) manually, I need a a script to do it automatically.
-
user177073 over 10 yearsgood question, I want to run it for each symbol, the symbols are nasdaq stock symbols I'm trying to pull a quote with the script.
-
-
rici over 10 yearsyou almost certainly want to either use
>>
or move the stdout redirection outside of the loop (I'd go for option 2). And($symbol)
will cause a bash error; I think you want to get rid of the parentheses. -
user177073 over 10 yearshere is what I changed it to, I know its not right as its not working; #!/bin/bash declare -a symbols=($(cat /home/user/Desktop/file5.txt | tr '\n' ' ')) for symbol in ${symbols[@]} do # echo "Processing $symbol ..." sh stock "$symbol" >> "$LOG_FILE" { lynx -dump "google.com/finance?client=ob&q=${1}" | sed 's/.*']'//' | perl -00ne "print if /Watch this stock/i" | sed 's/Watch this stock//' | sed 's/Disclaimer//' | sed '/^$/d' | sed 's/Currency in USD//' | LOG_FILE=/home/user/Desktop/stockquote.txt } /bin/bash /home/user/Desktop/quoteparse h
-
user177073 over 10 yearsalso when I run the script I'm getting this error: sh stock2 stock2: 2: stock2: Syntax error: "(" unexpected as it doesn't do any of the file manipulation I'm guessing its stopping at the first brace "(" in the script.
-
Smiley over 10 yearsThe code pasted in the comment earlier doesn't seem to be meaningful. Additionally, the arrays wouldn't work with sh -- use bash instead.
-
neowulf33 over 10 yearsThanks for the comment. I have reflected your suggestions in my answer.
-
user177073 over 10 yearsHere is what I've come up with so far: > /bin/bash #!/bin/bash # SCRIPT: method1.sh # PURPOSE: Process a file line by line with PIPED while-read loop. FILENAME=$ /home/user/Desktop/file5.txt count=0 cat $FILENAME | while read LINE do bin/bash stock $LINE | > /home/user/Desktop/$Line.txt let count++ echo “$count $LINE” done echo -e “\nTotal $count Lines read” while read -r line;do touch "$line" || echo "Couldn't create \"$Line\"" && exit 27 done < /home/user/Desktop/file5.txt As you can see this is a mashup of suggestions posted here, but it's not working, any ideas?
-
Smiley over 10 yearsUnless there was some typing mistake while posting the code in the above comment, this contains a few errors. Corrected lines -- 1)
FILENAME=/home/barun/Desktop/file5.txt
2)/bin/bash stock $LINE > /home/barun/Desktop/$Line.txt
-
user177073 over 10 yearsHere is the out put I'm getting , it's trying to read all the values in one sweep; $'ALOT\rCNIT\rCNYD\rCOBR\rDBLE\rDGLY\rECTE\rEMITF\rGAI\rGTXI\rHCIIP\rHOLL\rLACO,': command not found /home/user/Desktop/file5.txt: line 2: $'MGCD\rMICTW\rOCLS\rPBIB\rPFIN\rSHLD\rSPLK\rTRNS\rTSPT\rVISN\rXGTIW\rZGNX': command not found I need them read individually like this; /home/user/Desktop/file5.txt: line 1: $'ALOT' the script I'm using only takes one stock symbol at a time, runs it for a quote, so I need it to read the the lines separately not all at once.