I was wondering why it didn't work when I do:
echo "d_suites/k_val/tests/asm/logs/kf_on_stage1 FAILED 0:00:22 Jul 22 22:33 " |
sed 's/[ \t]*\([^ \t]+\)[ \t]+\([^ \t]+\).*/\2/'
but this one (change + to * ) works:
echo "d_suites/k_val/tests/asm/logs/kf_on_stage1 FAILED 0:00开发者_开发百科:22 Jul 22 22:33 " |
sed 's/[ \t]*\([^ \t]*\)[ \t]*\([^ \t]*\).*/\2/'
Any help will be appreciated.
Sed does not support the +
wildcard by default.
$ echo "aaabbbccc" | sed "s/a+/XXX/g"
aaabbbccc
You can enable it with the -r
flag (on GNU sed) or -E
flag (on Mac OS X and, I suspect, *BSD sed) because these options enable the use of extended regular expressions (in opposition to basic regular expressions):
$ echo "aaabbbccc" | sed -E "s/a+/XXX/g"
XXXbbbccc
If you use GNU sed, it supports the +
as a repeater in the basic regex mode if you escape it with a backslash:
$ echo "aaabbbccc" | sed "s/a\+/XXX/g"
The first one doesn't work because you need to escape your +'s, like this:
echo "d_suites/k_val/tests/asm/logs/kf_on_stage1 FAILED 0:00:22 Jul 22 22:33 " |
sed 's/[ \t]*\([^ \t]\+\)[ \t]\+\([^ \t]\+\).*/\2/'
Edit
For more info about why, read this very informative comment.
If you're not completely married to sed, awk would be more readable:
echo "..." | awk '{print $2}'
Edited - now referring to [^ \t]
[^ \t]+
is 1-n tabs, requiring there to be a non-tab.
[^ \t]*
is 0-n tabs, not requiring there to be a non-tab.
You don't have a non-tab in your input string there
精彩评论