변수가 줄의 일부와 일치하면 텍스트 파일의 줄을 표시하시겠습니까?

변수가 줄의 일부와 일치하면 텍스트 파일의 줄을 표시하시겠습니까?

재고검사기를 만들고 있어요. 모든 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.

나는 또한 모든 나스닥 거래소의 파일을 가지고 있습니다.

두 파일이 첫 번째 매개변수(입력된 주식 기호)와 일치하는지 확인하고 설명을 표시하려면 어떻게 해야 합니까? (텍스트 파일 이름 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는 원하는 순서대로 문장을 인쇄합니다.

관련 정보