Reputation: 3
Here's a bash script that extracts some data from a html file.
price=`grep ' <td>\$' $1 | sed -e 's/<td>//g' -e 's:</td>::g' -e 's/\$ //g' -e 's/^ *//g'`
grep ' <td>\$' $1 | sed -e 's/<td>//g' -e 's:</td>::g' -e 's/\$ //g' -e 's/^ *//g'
echo "Price: $price"
The sed part could use some help, but that's not the issue here. The problem is that, when I run the script, it should print the found value twice, right? But it prints it only once, the first time (Without the 'Price:'). What's the problem here?
Upvotes: 0
Views: 248
Reputation: 246744
A couple of comments about your use of sed:
-e 's/^ *//g'
-- you don't need the "g": your pattern is anchored at the beginning so it can only match once. Might as well look for tabs too: -e 's/^[[:space:]]\{1,\}//'
-e 's/<td>//g' -e 's:</td>::g'
can be collapsed into -e 's|</\{0,1\}td>||g'
Upvotes: 0
Reputation: 392833
I'm guessing that unlike the code shown, the assignment actually happens in a subshell and therefore is not visible (lost on exit of subshell)
I'm afraid you ran into a subshell issue that you donot show here. Post more code that you actually use if you can.
--- Sample:
unset price
price=1
echo $price # works
unset price
echo -n 1 | price=$(cat)
echo $price # works _not_
Upvotes: 0
Reputation: 75568
The first grep reads everything on standard input. Then, the second grep blocks trying to read from stdin.
Upvotes: 0
Reputation: 798456
The problem is that the string you're returning has a \r
in it, which returns the cursor to the first column before printing stuff out. Use od -c
to verify. And use a proper tool such as xmlstarlet
to make sure this doesn't happen.
Upvotes: 1