Pradeep Vairamani
Pradeep Vairamani

Reputation: 4302

Sed in cygwin issue

I am writing a sed script that selects the strings that contain [20

The command that I am using is the following:

sed -n "/\[20/p" "C:\test_scripts\new1.txt"

This is working with the sed in mks toolkit. However, when I try using this with the sed in Cygwin, I get the following error:

sed: -e expression #1, char 6: unterminated address regex

Upvotes: 0

Views: 1305

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753695

Diagnosis

At a guess, the Cygwin shell is removing the backslash in the sed script, so the sed command is seeing just /[20/p which should generate the error message you're seeing. You'd be able to confirm this by using either:

set -x
sed -n "/\[20/p" ...
set +x

Or:

echo sed -n "/\[20/p" ...

These should show you what the sed program sees (the set -x notation might wrap the whole argument in single quotes).

How to fix?

Several possibilities, most of which should work:

  • sed -n '/\[20/p' ...
  • sed -n "/\\[20/p' ...
  • Edit a file (sed.script) to contain /\[20/p (one line), and run: sed -n -f sed.script ...

The shell shouldn't mess with material inside single quotes. Doubling up the backslashes is the traditional way of dealing with problems with backslashes going missing. Using the sed script file is overkill in the context if either of the others works, but also prevents the shell from messing with what's in the sed script.

Upvotes: 2

Related Questions