変数が行の一部と一致する場合は、テキストファイルの行を表示しますか?

変数が行の一部と一致する場合は、テキストファイルの行を表示しますか?

在庫検査機を作っています。すべてのNYSEとその説明を含むファイルがあります。説明はファイルの最初のタブ入力から始まります。ファイル形式は次のとおりです。

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.
...

株式を入力したら、その株式の説明が次のように表示されるようにしたいと思います(入力stockchecker AB)。

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

私はまた、すべてのナスダック取引所のファイルを持っています。

2つのファイルが最初のパラメータ(入力された在庫記号)と一致することを確認し、説明を表示するにはどうすればよいですか? (テキストファイル名NYSE.txtNASDAQ.txt)コード(必要な場合):

#!/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

解決策は次のとおりです。

    #!/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}'

最初の grep は、記号で始まる行を検索します。 sed は、get によって出力されたファイル名の後の ":" を空白に置き換えます。 awkは必要な順序で文を印刷します。

おすすめ記事