使用sed删除特定字符之间的字符

时间:2019-02-24 20:31:24

标签: regex linux bash unix sed

我得到此段:

*Cast and characters * Bob Denver is Gilligan, the inept, accident-prone First Mate (affectionately known as "Little Buddy" by "the Shipper") of the SS Minnow. Denver was not the first choice to play Gilligan; actor Jerry Van Dyke, phone 210-222-3333, was offered the role on 2/11/1963, but he turned it down, believing that the show would never be successful. He chose instead to play the lead in My Mother the Car, which premiered the following year and was cancelled after one season. The producers looked to Bob Denver, the actor who had played Maynard G. Krebs, ss #111-22-3333, the goofy but lovable beatnik in The Many Loves of Dobie Gillis. None of the show's episodes ever specified Gilligan's full name or clearly indicated whether "Gilligan" was the character's first name or his last. In the DVD collection, Sherwood Schwartz states that he preferred the full name of "%Willy Gilligan%" for the character.

我的目标是使用sed将"%Willy Gillgan%"变成""。我已经尝试过s/%[^%]*%//,但是它也会干扰另一个将s/[0-9]{3}-[0-9]{2}-[0-9]{4}/%%%-%%/%%%%/更改为#111-22-3333的sed命令#%%%-%%-%%%%。它会错误地删除2%变成#%-%%-%%%%的地方。

以下是我的其他sed命令,以防它可能干扰其他内容: s+([0-9]{1,2})-([0-9]{1,2})-([0-9]{4})+\3-\2-\1+g转换日期格式

/[*]\s/i\ \n* ATTENTION *\n在段落中的任何地方遇到“ *”时,添加*注意*行和换行符。

这是我的脚本文件的样子:

s/[0-9]{3}-[0-9]{2}-[0-9]{4}/%%%-%%/%%%%/

s+([0-9]{1,2})-([0-9]{1,2})-([0-9]{4})+\3-\2-\1+g

s/%[^%]*%//

/[*]\s/i\ \n* ATTENTION *\n

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

尝试:

$ cat script.sed
s/%[^%]*%//g
s/[0-9]{3}-[0-9]{2}-[0-9]{4}/%%%-%%-%%%%/g
s+([0-9]{1,2})[/-]([0-9]{1,2})[/-]([0-9]{4})+\3-\2-\1+g
/[*]\s/i\
\n* ATTENTION *\n

这将产生输出:

$ sed -Ef script.sed text
*Cast and characters

* ATTENTION *

* Bob Denver is Gilligan, the inept, accident-prone First Mate
(affectionately known as "Little Buddy" by "the Shipper") of the SS Minnow. Denver was not the first choice to play Gilligan; actor Jerry Van Dyke, phone 210-222-3333, was offered the role on 1963-11-2, but he turned it down, believing that the show would never be successful. He chose instead to play the lead in My Mother the Car, which premiered the following year and was cancelled after one season. The producers looked to Bob Denver, the actor who had played Maynard G. Krebs, ss #%%%-%%-%%%%, the goofy but lovable beatnik in The Many Loves of Dobie Gillis. None of the show's episodes ever specified Gilligan's full name or clearly indicated whether "Gilligan" was the character's first name or his last. In the DVD collection, Sherwood Schwartz states that he preferred the full name of "" for the character.