5

I have a set of saved html files with links in them of the form http://mywebsite.com/showfile.cgi?key=somenumber but I want to kill the question mark (side-story is that firefox hates ? and randomly converts it to %3F I'm sure there's some magic fix but that's for another question...)

However, I think my code is causing the question-mark character to not be read/saved/handled properly when storing the options as a variable by bash

# Doesn't work (no pattern matched)
SED_OPTIONS='-i s/\.cgi\?key/\.cgikey/g'

# Works e.g. http://mywebsite.com/showfileblah?key=somenumber
SED_OPTIONS='-i s/\.cgi/blah/g'

# Leaves question mark in e.g. http://mywebsite.com/showfile.blah?key=somenumber
SED_OPTIONS='-i s/cgi\?/blah/g'

# Actual sed command run when using SED_OPTIONS (I define FILES earlier in
# the code)
sed $SED_OPTIONS $FILES

# Not using the SED_OPTIONS variable works
# e.g. http://mywebsite.com/showfile.cgikey=somenumber
sed -i s/\.cgi\?key/\.cgikey/g $FILES

How can I get the full command to work using the SED_OPTIONS variable?

2
  • -i s/cgi\?/blah/g should actually give you http://mywebsite.com/showfile.blahkey=somenumber. Jul 6, 2014 at 20:02
  • I know! If I define it with SED_OPTIONS='-i s/cgi\?/blah/g' it leaves the question mark in, but if I write the options inline in the same line as I call the sed command "sed -i s/cgi\?/blah/g $FILES" it removes the question mark Jul 6, 2014 at 20:06

2 Answers 2

7

The safest way to store a list of options and arguments in variables is to use an array:

Also:

  • You're using a basic regular expression (no -r or -E option), so ? is not a special char. and needs no escaping.
  • In the replacement string, which is not a regex, do not escape ..
  • No need for option g, since you're only replacing 1 occurrence per line.
# Create array with individual options/arguments.
SED_ARGS=( '-i' 's/\.cgi?key/.cgikey/' )

# Invoke `sed` with array - note the double-quoting.
sed "${SED_ARGS[@]}" $FILES

Similarly, it would be safer to use an array for the list of input files. $FILES will only work if the individual filenames contain no embedded whitespace or other elements subject to shell expansions.

Generally:

  • Single-quote string literals, such as the sed script here - to prevent the shell from interpreting them.
  • Double-quote variable references, to prevent the shell from performing additional operations on them, such as pathname expansion (globbing) and word splitting (splitting into multiple tokens by whitespace).
0
3

I suggest storing the arguments for sed in an array:

SED_OPTIONS=( '-i' '-e' 's/\.cgi?key/\.cgikey/g' )

sed "${SED_OPTIONS[@]}" $FILES

However, that's only a part of the trouble.

First, note that when you type:

sed -i s/\.cgi\?key/\.cgikey/g $FILES

what sed sees as the script argument is actually:

s/.cgi?key/.cgikey/g

because you didn't use any quotes to preserve the backslashes. (To demonstrate, use printf "%s\n" s/\.cgi\?key/\.cgikey/g, thus avoiding any questions of whether echo is interpreting the backslashes.) One side effect of this is that a URL such as:

http://example.com/nodotcgi?key=value

will be mapped to:

http://example.com/nodo.cgikey=value

Using the single quotes when setting SED_OPTIONS ensures that the backslashes are preserved where required, and not putting a backslash before the ? works. I have both GNU sed and BSD sed on my Mac; I've aliased them as gnu-sed and bsd-sed for clarity. Note that BSD sed requires a suffix for -i and won't accept standard input with -i. So, I've dropped the -i from the commands.

$ URLS=(http://example.com/script.cgi?key=value http://example.com/nodotcgi?key=value)
$ SED_OPTIONS=( '-e' 's/\.cgi?key/\.cgikey/g' )
$ printf "%s\n" "${URLS[@]}" | bsd-sed "${SED_OPTIONS[@]}"
http://example.com/script.cgikey=value
http://example.com/nodotcgi?key=value
$ printf "%s\n" "${URLS[@]}" | gnu-sed "${SED_OPTIONS[@]}"
http://example.com/script.cgikey=value
http://example.com/nodotcgi?key=value
$ SED_OPTIONS=( '-e' 's/\.cgi\?key/\.cgikey/g' )
$ printf "%s\n" "${URLS[@]}" | bsd-sed "${SED_OPTIONS[@]}"
http://example.com/script.cgikey=value
http://example.com/nodotcgi?key=value
$ printf "%s\n" "${URLS[@]}" | gnu-sed "${SED_OPTIONS[@]}"
http://example.com/script.cgi?key=value
http://example.com/nodotcgi?key=value
$

Note the difference in behaviour between the two versions of sed when there's a backslash before the question mark (second part of the example).

8
  • Thank you for the detailed answer - If you have $ SED_OPTIONS=( '-e' 's/\.cgi?key/\.cgikey/g' ) won't the question mark in the regex be interpreted as "you need zero or one of 'i'" and not as the actual symbol for a question mark? Jul 6, 2014 at 20:56
  • 1
    As you can see, the answer is 'No' (? is not interpreted as one-or-more of i). In standard (classic) sed regexes, ? is not a metacharacter. If you use extended regular expressions or similar, then you run into a different set of problems, but basic sed should/does use basic regular expressions where ? is not a metacharacter. Jul 6, 2014 at 21:02
  • Do you know what effect the \ before the dot in the replacement string does, as it's not a regex as noted by @mklement0 but when I try the replacement string with/without it I can't see any difference in behaviour? Jul 6, 2014 at 21:31
  • 1
    I think it means that the next character is interpreted as itself rather than as a metacharacter, but the number of metacharacters in the replacement string is strictly limited (there's backslash itself, and \n for newline in sed, and then \1 etc for replacing captures, and that's close to it). So, both \. and . replace as .. Jul 6, 2014 at 21:46
  • 1
    Yes; GNU sed is a bit more consistent (with itself) than the standard is. Having \n mean one thing on the LHS and a different thing on the RHS of the s/// command is a little irritating. Jul 7, 2014 at 1:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.