如何匹配除sed的正则表达式之外的所有内容?

时间:2016-05-30 14:22:49

标签: regex sed

使用sed给出以下结果:

~ echo drive kids to the Foo Bar +09:00-10:00 | sed 's/\ *+[a-zA-Z0-9\{._\-\:\-}]*\ */ /g'
drive kids to the Foo Bar -10:00
~ echo drive kids to the Foo Bar +09:00-10:00 | sed 's/\ *+[a-zA-Z0-9\{._\-\}]*\ */ /g'
drive kids to the Foo Bar :00-10:00
~ echo pick suzieq up from +10:30-11:00 kung foobar practice | sed 's/\ *+[a-zA-Z0-9\{._\-\}]*\ */ /g'
pick suzieq up from :30-11:00 kung foobar practice
~ echo +11:15-12 pick kids and new friends up from Foo Bar | sed 's/\ *+[a-zA-Z0-9\{._\-\}]*\ */ /g'
 :15-12 pick kids and new friends up from Foo Bar

我想要的是:

~ echo drive kids to the Foo Bar +09:00-10:00 | sed 'some regular expression'
drive kids to the Foo Bar 
~ echo drive kids to the Foo Bar +09:00-10:00 | sed 'some regular expression'
drive kids to the Foo Bar 
~ echo pick suzieq up from +10:30-11:00 kung foobar practice | sed 'some regular expression'
pick suzieq up from kung foobar practice
~ echo +11:15-12 pick kids and new friends up from Foo Bar | sed 'some regular expression'
 pick kids and new friends up from Foo Bar

作为旁注: 我有a regular expression可以在+符号后选择我想要的所有内容,但是我如何将我在rubular中所做的内容翻译为sed,以便sed将:“选择除正则表达式之外的所有内容”?谢谢你的帮助:)

编辑:

~/sandbox/sed-practice echo drive kids to the Foo Bar +09:00-10:00 | sed 's/([+][^[:space:]]+)?./\1/g'
sed: 1: "s/([+][^[:space:]]+)?./ ...": \1 not defined in the RE

~/sandbox/sed-practice echo drive kids to the Foo Bar +09:00-10:00 | sed -r 's/([+][^[:space:]]+)?./\1/g'
sed: illegal option -- r
usage: sed script [-Ealn] [-i extension] [file ...]
       sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]

2 个答案:

答案 0 :(得分:1)

用!

扭转限制

有时您需要在除了与正则表达式匹配的行之外的每一行上执行操作,或者在一系列地址之外执行操作。 "!"字符,通常意味着不在UNIX实用程序中,反转地址限制。你记得那个

sed -n '/match/ p'

就像grep命令一样。 " -v" grep选项打印所有不包含该模式的行。 Sed可以用

做到这一点
sed -n '/match/ !p'

找到答案here

答案 1 :(得分:1)

您可以使用此sed命令:

# original input
cat file
drive kids to the Foo Bar +09:00-10:00
pick suzieq up from +10:30-11:00 kung foobar practice
+11:15-12 pick kids and new friends up from Foo Bar

# sed command
sed 's/\(.*\)+[^[:space:]]*[[:blank:]]*/\1/g' file

drive kids to the Foo Bar
pick suzieq up from kung foobar practice
pick kids and new friends up from Foo Bar
  • [[:blank:]]匹配空格或标签
  • [[:space:]]匹配空格或制表符或换行符