开发者

Is there any way around using temporary files when you want to extract discontinuous lines?

开发者 https://www.devze.com 2023-02-27 02:20 出处:网络
Here\'s a bash script I wrote to get the weather for Boston (the weather-util command recently broke for Boston, MA):

Here's a bash script I wrote to get the weather for Boston (the weather-util command recently broke for Boston, MA):

#!/bin/bash

# Shows the weather forecast开发者_如何学JAVA for Boston, MA

TMPFILE=${TMPDIR-/tmp}/ne_weather.$$
curl -s 'http://weather.noaa.gov/pub/data/forecasts/state/ma/maz007.txt' > $TMPFILE
sed -n '/^TAB/,+11p' < $TMPFILE
sed -n '/BOSTON/,+3p' < $TMPFILE
rm $TMPFILE

I am using a temp file because I can't figure out how to use sed on the output of curl in one pass. Any suggestions for doing this without resorting to a temporary file? Or is it perfectly OK?


You can pipe the output of curl into sed:

curl -s 'http://weather.noaa.gov/pub/data/forecasts/state/ma/maz007.txt' | sed -n '/^TAB/,+11p; /BOSTON/,+3p'


If you don't want a temp file, just display?

while read -r line
do
    case "$line" in
        "TAB"* )
        echo "->$line";
        for i in {1..11};do read -r line;echo "->$line"; done
        ;;
        "BOSTON"* )
        echo "$line"
        for i in {1..3}; do read -r line ; echo "$line";done
        ;;
    esac
done < <(curl -s 'http://weather.noaa.gov/pub/data/forecasts/state/ma/maz007.txt')
0

精彩评论

暂无评论...
验证码 换一张
取 消