1

Given a text like this

./RFF_09 -f${FILE} -c${COND}

inside a file, this egrep command will correctly match:

egrep './RFF(.*) (.*)-c\\$\{COND\}' file

but this sed command will not

sed -n "s:'./RFF(.*) (.*)-c\\$\{COND\}':./RFF$1 $2-cRFF$1:gp"

It will fail with sed: -e expression #1, char 38: invalid content of \{\}. I've also tried with

sed -n "s:'./RFF(.*) (.*)-c\\$\{COND\}':DUMMY:gp" file
sed -n s:'./RFF(.*) (.*)-c\\$\{COND\}':DUMMY:gp file

with the same result.

sed -n "s:'./RFF(.*) (.*)-c\\$\\{COND\\}':DUMMY:gp" file

Will not give me an error message, but will not match.

What am I doing wrong? Or better: How can I replace the text as suggested by the original command? I'm using rather old versions of sed (4.1.2) and egrep (2.5.1), so a workaround is appreciated even if you can't reproduce the error with newer versions.

2 Answers 2

2

If you use egrep that mean grep with extended regexp syntax so to be able to transfer your pattern into sed you have to add parametr -r(--regexp-extended) or -E in some versions.

Regarding to your expression you have extra \ after c so even with egrep it does not match

Additionally your better use \1 instead $1 for reverse-matching.

So final command could be:

sed -rn 's:\./(RFF.*)( .* -c)\$\{COND\}:./\1\2\1:gp'

Or

sed -rn 's:(\./(RFF\S*) \S* -c)\$\{COND\}:\1\2:gp'
2
  • Great, this works. Yes, the extra \ is a typo, it was from when the expression had no '' to double-escape the $ from both bash and regex.
    – Chaos_99
    Commented Feb 2, 2015 at 10:57
  • @Chaos_99 Just single qoute (') have escape effect. Double-quote expand variables (which usually been marked by $) instead.
    – Costas
    Commented Feb 2, 2015 at 11:42
2

In your original statement the problem is that you're mixing syntactic and literal quotes: the single quotes within the double quotes will be matched literally. You'll want to either simply remove them or mix quotes (not nest them). Untested examples:

sed 's/foo/{bar}/'
sed "s/foo/"'{bar}'"/"

There are many flavours of regular expressions, and they all support different syntax. In the attempt using only single quotes the issue is the regex flavour: sed uses \{N\} for match counts, so N needs to be an integer. You'll want to instead use {COND}.

(Truly, using a regex to solve a problem now means you have two problems. And doing pretty much anything complex in shell code means you have N problems, N being at least as large as the number of lines.)

4
  • Only using "" or '' on the outside as well as switching the quotes '""' instead of "''" still gives my the invalid content message. Using no quotes will break at unexpected token near '(' (probably the blank space). How do I quote this correctly?
    – Chaos_99
    Commented Jan 28, 2015 at 15:52
  • Please read the linked article about quoting.
    – l0b0
    Commented Jan 28, 2015 at 15:56
  • I did read the linked article. It did not help me. Single quotes should solve my problem, but they don't. Probably because this will be evaluated more than once. Double single-quotes didn't help either. I would be thankful for an example.
    – Chaos_99
    Commented Jan 28, 2015 at 16:16
  • What do you mean it "will be evaluated more than once"? And I never even mentioned double single quotes.
    – l0b0
    Commented Jan 28, 2015 at 16:20

You must log in to answer this question.

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