我正在做一个库存检查器。我有一个包含所有纽约证券交易所及其描述的文件。描述从文件的第一个选项卡输入开始。文件格式如下所示:
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.
我还有一份所有纳斯达克交易所的文件。
如何检查两个文件以查看其是否与第一个参数(输入的股票代码)匹配,然后显示说明? (文本文件名为NYSE.txt
和NASDAQ.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 按照您想要的顺序打印句子。