1

I'm making a stock checker. I have a file with all of the NYSE exchanges and their descriptions. The descriptions start on the first tab enter of the file. The file formatting looks like this:

A       Agilent Technologies
AA      Alcoa Inc
AA-B    Alcoa Inc.
AAC     Aac Holdings Inc.
AAN     Aaron's Inc
AAP     Advance Auto Parts Inc
AAT     American Assets Trust
AAV     Advantage Oil & Gas Ltd
AB      Alliance Capital Management L.P.
ABB     Abb Ltd
ABBV    Abbvie Inc. Common Stock
ABC     Amerisourcebergen Corp
ABEV    Ambev S.A.
...

I want it so when a stock is entered, it shows the description of the stock with it, like this (input stockchecker AB):

Stock: AB (NYSE) - Alliance Capital Management L.P.

I also have a file with all of the NASDAQ exchanges.

How can I check both files to see if it matches the first argument (the stock symbol entered), and then display the description? (The text files are named NYSE.txt and NASDAQ.txt) Code (if needed):

#!/bin/bash
stock=$1
touch info.txt # create info.txt if missing
touch raw-info.txt # create raw-info.txt if missing
echo $( wget http://www.google.com/finance/info?q=NASDAQ%3a$stock -q -O -) > raw-info.txt # get the information from Google Finance and write it to info.txt
tr "\" ," "\n" < raw-info.txt > info.txt # split the information from Google Finance into separate lines
##########set stock variables############
stockID=$(sed '8q;d' < info.txt)         #
stockTicker=$(sed '16q;d' < info.txt)    #
stockCorp=$(sed '24q;d' < info.txt)      #
stockPrice=$(sed '48q;d' < info.txt)     #
lastUpdate=$(sed '73q;d' < info.txt)     #
priceChange=$(sed '90q;d' < info.txt)    #
percentChange=$(sed '106q;d' < info.txt) #
previousClose=$(sed '130q;d' < info.txt) #
ahPrice=$(sed '151q;d' < info.txt)       #
ahLUpdate=$(sed '162q;d' < info.txt)     #
ahPriceChange=$(sed '171q;d' < info.txt) #
ahPctChange=$(sed '187q;d' < info.txt)   #
#########################################
#ah color formatting
linecount=$(wc -l info.txt)
linecount=${linecount#?} #remove tab character from wc -l
linecount=${linecount%?????????} #remove info.txt from wc -l
if [[ $linecount -gt 150 ]] ; then
    if [[ ${ahPriceChange:0:1} == "+" ]] ; then ahPriceChange=${ahPriceChange#?} ; ahPriceChange=$(printf "\e[32m+$ahPriceChange\e[0m") ; fi
    if [[ ${ahPriceChange:0:1} == "-" ]] ; then ahPriceChange=${ahPriceChange#?} ; ahPriceChange=$(printf "\e[31m-$ahPriceChange\e[0m") ; fi
    if [[ ${ahPctChange:0:1} != "-" ]] ; then ahPctChange=$(printf "\e[32m+$ahPctChange%%\e[0m") ; fi
    if [[ ${ahPctChange:0:1} == "-" ]] ; then ahPctChange=${ahPctChange#?} ; ahPctChange=$(printf "\e[31m-$ahPctChange%%\e[0m") ; fi
    ah_trades_present="yes"
else
    ah_trades_present="no"
fi
#color formatting
if [[ ${priceChange:0:1} == "+" ]] ; then priceChange=${priceChange#?} ; priceChange=$(printf "\e[32m+$priceChange\e[0m") ; fi
if [[ ${priceChange:0:1} == "-" ]] ; then priceChange=${priceChange#?} ; priceChange=$(printf "\e[31m-$priceChange\e[0m") ; fi
if [[ ${percentChange:0:1} != "-" ]] ; then percentChange=$(printf "\e[32m+$percentChange%%\e[0m") ; fi
if [[ ${percentChange:0:1} == "-" ]] ; then percentChange=${percentChange#?} ; percentChange=$(printf "\e[31m-$percentChange%%\e[0m") ; fi

if [[ $ah_trades_present == "no" ]] ; then
echo    "Google Finance ID:    $stockID                      "
echo    "Stock                 $stockTicker ($stockCorp)     "
echo    "Price                 $stockPrice                   "
echo    "Change:               $priceChange [$percentChange] "
echo    "Previous Close:       $previousClose                "
echo    "Last Update:          $lastUpdate               "
fi
if [[ $ah_trades_present == "yes" ]] ; then
echo    "#################### AFTER HOURS ###################"
echo    "Google Finance ID:    $stockID                      "
echo    "Stock                 $stockTicker ($stockCorp)     "
echo    "Price:                $ahPrice                  "
echo    "Change:               $ahPriceChange [$ahPctChange] "
echo    "Last Update:          $ahLUpdate            "
echo    "Previous Close:       $stockPrice           "
fi
  • 1
    check this out. – mikeserv Jul 3 '15 at 4:04
  • @mikeserv Thanks, got that working by wgetting the URL provided for receiving stock data with the stock symbol provided in $1 and then removing the other redundancy just to get the name by using sed 's/,.*//'. – 智障的人 Jul 3 '15 at 4:42
1

Here is one solution -

    #!/bin/bash
    grep "^$1 " NYSE.txt NASDAQ.txt | sed 's/:/ /' | awk '{printf "Stock %s ( %s ) -  ",$2,$1; for(i=3;i<NF;i++) printf "%s ",$i OFS;if(NF)printf"%s",$NF;printf ORS}'

The first grep searches for the line that starts with symbol. The sed substitutes the ":" after file name from get output to a blank space. The awk prints the sentence in the order you desire.

  • Not getting any output. – 智障的人 Jul 3 '15 at 4:24
  • what are you trying ... it is working for me alright... as you mentioned, you need to pass the stock symbol as the argument – amisax Jul 3 '15 at 4:34
  • Nevermind, I got it to work with another method. Thanks though! – 智障的人 Jul 3 '15 at 4:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.