在 Bash 中删除和替换字符串的中间部分

在 Bash 中删除和替换字符串的中间部分

我有几条字符串:

Central and Western</td><td><a href="javascript:pop1('major-housing-estate/30006.html');" title="Kwun Lung Lau">Kwun Lung Lau

Central and Western</td><td><a href="javascript:pop1('major-housing-estate/61782.html');" title="The Merton">The Merton

Eastern</td><td><a href="javascript:pop1('major-housing-estate/10038.html');" title="Fung Wah Estate">Fung Wah Estate

Wong Tai Sin</td><td><a href="javascript:pop1('major-housing-estate/20134.html');" title="Tsz Oi Court">Tsz Oi Court

我想删除并替换中间部分:

从:

</td><td><a href="javascript:


到:

">

我想用以下字符替换整个部分:

;

我需要我的琴弦变成:

Central and Western;Kwun Lung Lau

Central and Western;The Merton

Eastern;Fung Wah Estate

Wong Tai Sin;Tsz Oi Court

我如何以通用方式做到这一点?

答案1

如果您的字符串位于名为的文件中file

$ sed 's|</td><td><a href="javascript:[^>]*">|;|g' file
Central and Western;Kwun Lung Lau
Central and Western;The Merton
Eastern;Fung Wah Estate
Wong Tai Sin;Tsz Oi Court

如果你的字符串位于名为的 shell 变量中s

$ echo "$s"
Central and Western</td><td><a href="javascript:pop1('major-housing-estate/30006.html');" title="Kwun Lung Lau">Kwun Lung Lau
$ echo "$s" | sed 's|</td><td><a href="javascript:[^>]*">|;|g'
Central and Western;Kwun Lung Lau

相关内容