Is there any way around using temporary files when you want to extract discontinuous lines?
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')
精彩评论