0

I am having trouble with a sed edit. When I run:

close=`curl 'https://finance.yahoo.com/quote/INTC?p=INTC&.tsrc=fin-srch'|html2text|sed -n '/Add to watchlist/,$p'|sed -n '/At close\:/q;p'|head -n-1|tail -n+3`; echo $close

I get: 45.60+0.21 (+0.46%)

But when I add an additional sed command, to strip away everything but the floating number before either the first + or -, like so:

close=`curl 'https://finance.yahoo.com/quote/INTC?p=INTC&.tsrc=fin-srch'|html2text|sed -n '/Add to watchlist/,$p'|sed -n '/At close\:/q;p'|head -n-1|tail -n+3|sed -n '/^\([0-9]*\.[0-9]*\)[+-].*$/\1/'`; echo $close

my output (echo) is empty. Any ideas what I am missing?

5
  • 1
    sed -n? Why -n?
    – muru
    Commented Nov 10, 2020 at 5:04
  • because the result should pipe instead of print
    – naphelge
    Commented Nov 10, 2020 at 5:27
  • 2
    If it's not printing anything, there's nothing to pipe.
    – muru
    Commented Nov 10, 2020 at 5:46
  • So really what you're trying to debug is echo '45.60+0.21 (+0.46%)' | sed -n '/^\([0-9]*\.[0-9]*\)[+-].*$/\1/' Commented Nov 10, 2020 at 9:59
  • The -n has nothing to do with piping instead of printing. It tells sed only to generate output if there's an explicit print instruction Commented Nov 10, 2020 at 10:06

1 Answer 1

0

You can simplify the entire pipeline

curl ... |
    html2text|
    sed -n '/Add to watchlist/,/At close/s/^\([0-9][0-9]*\.[0-9][0-9]*\)[-+].*/\1/p'

If you have GNU sed you can simplify the expression a little by using its more powerful RE engine

sed -rn '/Add to watchlist/,/At close/s/^([0-9]+\.[0-9]+)[-+].*/\1/p'

The sed command looks only on lines including and between "Add to watchlist" and "At close". In that range it looks for a string comprising one or more digits, a dot, and one or more digits, such that it is followed by either + or -.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .