Sed regex problem on Mac, works fine on Linux
This works fine on Linux (Debian):
sed -e 's,^[ \t]*psd\(.*\)\;,,'
On mac, I believe I have to use the -E
flag, instead of -e
:
sed -E 's,^[ \t]*psd\(.*\)\;,,'
but the regexp does not match, and hence does not remove the lines I want.
Any tips on how to solve this?
Sample input:
apa
bepa
psd(cepa);
depa psd(epa);
psd(fepa gepa hepa);
For that input, the expected output is:
apa
bepa
depa p开发者_C百科sd(epa);
The -E
flag means to use extended regular expressions. You should just use -e
, as on Linux. The sed
in Mac OS X is based on BSD sed, so doesn't have the GNU extensions.
After copying your sample input:
[~ 507] pbpaste | sed -e 's,^[[:space:]]*psd\(.*\);,,'
apa
bepa
depa psd(epa);
Alternatively you can use the GNU version of sed instead of the implementation provided by Mac OSX.
Mac port provides a port for it sudo port install gsed
. After installing it you can use gsed
instead of sed
.
The '\t'
is not standard in 'sed'
, it is a GNU extension.
To match a 'tab'
, you need to put a real 'tab'
in your script. This is easy in a file, harder in shell.
The same problem can happen in AIX, Solaris and HP-UX or other UNIXes.
In addition to the answers above, you can exploit a useful (but shell-dependent) trick. In bash, use $'\t'
to introduce a literal tab character. The following works on my Mac:
sed -e 's,^[ '$'\t''*psd\(.*\);,,'
Note how the whole sed expression consists now of three concatenated strings.
This trick might be useful in case you need the tab character specifically, without matching other whitespace (i.e., when [[:blank:]]
would be too inclusive). For the above, the -e flag is not essential.
I've check this sample input on my machine and faced the problem when in third line was tab character from the beginning of line and regexp ^[ \t]*psd\(.*\)\;
didn't match it. This can be passed by sed character class [[:blank:]]
that equal combination of space and tab character. So you can try the following:
sed -E 's,^[[:blank:]]*psd\(.*\)\;,,' demo.txt
this produce the following output:
apa
bepa
depa psd(epa);
but it keeps the empty lines in result. To get the exact output as you expected I used the following:
sed -n '/^[[:blank:]]*psd\(.*\)\;/!p' demo.txt
result:
apa
bepa
depa psd(epa);
this is just inverse output of matching pattern (!p
).
EDIT: To match tab characters in regexp in sed (macosx) you can also try recommendation from How can I insert a tab character with sed on OS X?
精彩评论