I have a line similar to this
grep -oP "data-context-item-title=.*.data-context-item-id" web.html | cut -d'"' -f2
I know this line correctly works since I used it on the terminal and it gave me the desired output. However, I want to put this line in a bash script. so I have this so far
title="$(grep -oP 'data-context-item-title=.*.data-context-item-id' web.html | cut -d'"' -f2)"
This is a problem because it matched the first “(quotation) with the cut’s “(quotation). Is there anyway to avoid it?
Output without the cut function is something similar to this
data-context-item-title="Some long title" data-context-item-id
data-context-item-title="Another very long title" data-context-item-id
Keep in mind, I can’t use any sed or awk commands to replace cut.
Thanks
Since you’re using bash, it would be safe enough to use:
This will preserve the internal spacing (in particular newlines) in the variable, as you’d be able to see if you did:
I’m of the opinion that if you managed to find a UNIX™ 7th Edition Bourne Shell to use, then omitting the double quotes around the
$(...)notation (or, more accurately, the`...`notation) would not be safe, but it does seem to work safely on modern shells (say those last updated in the current millennium, rather than in the previous one). The difficulty is in finding an old Bourne Shell on which to verify my now shaky (because distant) recollection.What puzzles me, though, is that with both
bash3.2 (system) and 4.2 (home-built) running on Mac OS X 10.7.5, your code works correctly for me both with and without the double quotes around the$(...). Which version ofbashare you using, and on which platform?