user1709294
user1709294

Reputation: 1795

Bash Using Quotes in Grep/Cut Commands in Scripts

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

Upvotes: 2

Views: 4979

Answers (5)

glenn jackman
glenn jackman

Reputation: 246799

First, to solve the error escape the double quote for cut:

title="$(... | cut -d \" ...)"

However, you're using grep's PCRE, so you can use lookarounds and drop cut altogether:

title=$(grep -oP '(?<=data-context-item-title=").*?(?=" data-context-item-id)' web.html)

Upvotes: 0

Gordon Davisson
Gordon Davisson

Reputation: 125788

Wild suggestion: is it possible one of the quotes in your command isn't a plain ASCII quote, but some sort of Unicode fancy quote (which the shell won't recognize)?

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 753665

Since you're using bash, it would be safe enough to use:

title=$(grep -oP 'data-context-item-title=.*.data-context-item-id' web.html | cut -d'"' -f2)

This will preserve the internal spacing (in particular newlines) in the variable, as you'd be able to see if you did:

echo "$title"

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 bash 3.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 of bash are you using, and on which platform?

Upvotes: 3

doubleDown
doubleDown

Reputation: 8398

Not sure if it's a typo or not but you seemed to have forgotten the closing parenthesis for $(...)

Upvotes: -1

cowboydan
cowboydan

Reputation: 1102

The parameter for delimiter in cut is a double quote not single. Use backlash to escape the actual quote

Upvotes: 0

Related Questions