用 sed 替换文本

发布于 2024-12-19 00:47:03 字数 425 浏览 6 评论 0原文

程序从数据库创建 HTML 文件。有标题和标题之间的内容。

没有固定数量的标题。 在每个标题之后,程序都会放置文本:

$WHITE*("5")$ 

$WHITE*("20")$ 
$HRULE$ 

我需要将这 4 行的每次出现替换为:

$WHITE*("20")$ 
$HRULE$ 
$WHITE*("10")$ 

我不关心使用什么程序:)

我已经尝试过:

sed 's:\$WHITE\*(\"5\")\$\n\n\$WHITE\*(\"20\")\$\n\$HRULE\$:\$WHITE\*(\"20\")\$\
\$HRULE$\
\$WHITE*("10")$:g'

以及各种其他排列

A program creates HTML files from a database. There are headings and stuff in between the headings.

There are not a set amount of headings.
After each heading the program places the text:

$WHITE*("5")$ 

$WHITE*("20")$ 
$HRULE$ 

I need every occurrence of these 4 lines to be replaced with:

$WHITE*("20")$ 
$HRULE$ 
$WHITE*("10")$ 

I am not fussed what program is used :)

I have tried:

sed 's:\$WHITE\*(\"5\")\$\n\n\$WHITE\*(\"20\")\$\n\$HRULE\$:\$WHITE\*(\"20\")\$\
\$HRULE$\
\$WHITE*("10")$:g'

and various other permutations

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柒七 2024-12-26 00:47:03

如果这是您的输入文件,并且这是规范,您可以这样做:

sed -n '3,$p;$a$WHITE*("10")

但我认为情况并非如此,因此您可能需要重新表述您的问题和/或提供更多详细信息。

使用 sed 的更具体的解决方案:(

 sed '/^\$WHITE\*("5")\$/,/^$/d;/\$HRULE\$/ a$WHITE*("10")

搜索 $WHITE*("5")$ 行并将其删除,直到(包括!)下一个空行。然后搜索下一个 $HRULE $ 行并附加 $WHITE*("10")$

awk 解决方案:

 awk '/\$WHITE\*\("5"\)\$/ { getline ; next } 
      /\$WHITE\*\("20"\)\$/ { print ; 
                              getline ; 
                              if ($0 ~ /\$HRULE\$/) { print ; 
                                                      print "$WHITE*(\"10\")$" ;
                                                    }
                              else { print }
                            }
      1 ' INPUTFILE 

这会读取文件并打印每一行 - 这就是 $ WHITE*("10")$ 行。代码>1是在那里,除非它找到 $WHITE*("5") 模式,否则它会删除它,读取下一行,如果找到 $WHITE*("20" ,也会删除它。 ) 读取下一行,如果是 $HRULE$ 则打印该行以及附加的 $WHITE*("10") 行。 行。

打印HTH

INPUTFILE

但我认为情况并非如此,因此您可能需要重新表述您的问题和/或提供更多详细信息。

使用 sed 的更具体的解决方案:(


搜索 $WHITE*("5")$ 行并将其删除,直到(包括!)下一个空行。然后搜索下一个 $HRULE $ 行并附加 $WHITE*("10")$

awk 解决方案:


这会读取文件并打印每一行 - 这就是 $ WHITE*("10")$ 行。代码>1是在那里,除非它找到 $WHITE*("5") 模式,否则它会删除它,读取下一行,如果找到 $WHITE*("20" ,也会删除它。 ) 读取下一行,如果是 $HRULE$ 则打印该行以及附加的 $WHITE*("10") 行。 行。

打印HTH

INPUTFILE

搜索 $WHITE*("5")$ 行并将其删除,直到(包括!)下一个空行。然后搜索下一个 $HRULE $ 行并附加 $WHITE*("10")$

awk 解决方案:

这会读取文件并打印每一行 - 这就是 $ WHITE*("10")$ 行。代码>1是在那里,除非它找到 $WHITE*("5") 模式,否则它会删除它,读取下一行,如果找到 $WHITE*("20" ,也会删除它。 ) 读取下一行,如果是 $HRULE$ 则打印该行以及附加的 $WHITE*("10") 行。 行。

打印HTH

INPUTFILE

但我认为情况并非如此,因此您可能需要重新表述您的问题和/或提供更多详细信息。

使用 sed 的更具体的解决方案:(

搜索 $WHITE*("5")$ 行并将其删除,直到(包括!)下一个空行。然后搜索下一个 $HRULE $ 行并附加 $WHITE*("10")$

awk 解决方案:

这会读取文件并打印每一行 - 这就是 $ WHITE*("10")$ 行。代码>1是在那里,除非它找到 $WHITE*("5") 模式,否则它会删除它,读取下一行,如果找到 $WHITE*("20" ,也会删除它。 ) 读取下一行,如果是 $HRULE$ 则打印该行以及附加的 $WHITE*("10") 行。 行。

打印HTH

If that'S your input file, and this is the spec, you can do:

sed -n '3,$p;$a$WHITE*("10")

But I assume that's not the case, so you might want to rephrase your question and/or giving some more detailes.

More specific solution with sed:

 sed '/^\$WHITE\*("5")\$/,/^$/d;/\$HRULE\$/ a$WHITE*("10")

(Searches for the $WHITE*("5")$ line and deletes it till (including!) the next empty line. Then searches for the next $HRULE$ line and appends an $WHITE*("10")$ line.

awk solution:

 awk '/\$WHITE\*\("5"\)\$/ { getline ; next } 
      /\$WHITE\*\("20"\)\$/ { print ; 
                              getline ; 
                              if ($0 ~ /\$HRULE\$/) { print ; 
                                                      print "$WHITE*(\"10\")$" ;
                                                    }
                              else { print }
                            }
      1 ' INPUTFILE 

This reads the file and prints every line - that's why the 1 is there, except if it finds the $WHITE*("5") pattern it drops it, reads the next line and drops that too. if it finds the $WHITE*("20") prints it. Reads the next line and if its $HRULE$ then prints that and the appended $WHITE*("10") line. Else just prints the line.

HTH

INPUTFILE

But I assume that's not the case, so you might want to rephrase your question and/or giving some more detailes.

More specific solution with sed:


(Searches for the $WHITE*("5")$ line and deletes it till (including!) the next empty line. Then searches for the next $HRULE$ line and appends an $WHITE*("10")$ line.

awk solution:


This reads the file and prints every line - that's why the 1 is there, except if it finds the $WHITE*("5") pattern it drops it, reads the next line and drops that too. if it finds the $WHITE*("20") prints it. Reads the next line and if its $HRULE$ then prints that and the appended $WHITE*("10") line. Else just prints the line.

HTH

INPUTFILE

(Searches for the $WHITE*("5")$ line and deletes it till (including!) the next empty line. Then searches for the next $HRULE$ line and appends an $WHITE*("10")$ line.

awk solution:

This reads the file and prints every line - that's why the 1 is there, except if it finds the $WHITE*("5") pattern it drops it, reads the next line and drops that too. if it finds the $WHITE*("20") prints it. Reads the next line and if its $HRULE$ then prints that and the appended $WHITE*("10") line. Else just prints the line.

HTH

INPUTFILE

But I assume that's not the case, so you might want to rephrase your question and/or giving some more detailes.

More specific solution with sed:

(Searches for the $WHITE*("5")$ line and deletes it till (including!) the next empty line. Then searches for the next $HRULE$ line and appends an $WHITE*("10")$ line.

awk solution:

This reads the file and prints every line - that's why the 1 is there, except if it finds the $WHITE*("5") pattern it drops it, reads the next line and drops that too. if it finds the $WHITE*("20") prints it. Reads the next line and if its $HRULE$ then prints that and the appended $WHITE*("10") line. Else just prints the line.

HTH

一萌ing 2024-12-26 00:47:03

更新 #2

来自 sed 常见问题解答,部分 4.23.3

如果您需要匹配静态文本块(在整个文件中可能出现任意多次),并且该块的内容预先已知,则此脚本很容易使用

UPDATE #1

Python?

$ cat input
first line
second line
3rd line
$WHITE*("5")$

$WHITE*("20")$
$HRULE$
some more lines
yet another
$WHITE*("5")$

$WHITE*("20")$
$HRULE$
THE END

脚本:

#!/usr/bin/env python

## Use these 3 lines for python version < 2.5
#fd=open('input')
#text=fd.read()
#fd.close()

## Use these 2 lines for python version >= 2.5
with open('input') as fd:
    text=fd.read()

old="""$WHITE*("5")$

$WHITE*("20")$
$HRULE$
"""

new="""$WHITE*("20")$
$HRULE$
$WHITE*("10")$
"""

print text.replace(old,new)

输出:

first line
second line
3rd line
$WHITE*("20")$
$HRULE$
$WHITE*("10")$
some more lines
yet another
$WHITE*("20")$
$HRULE$
$WHITE*("10")$
THE END

UPDATE #2

From the sed faq, section 4.23.3

If you need to match a static block of text (which may occur any number of times throughout a file), where the contents of the block are known in advance, then this script is easy to use

UPDATE #1

Python?

$ cat input
first line
second line
3rd line
$WHITE*("5")$

$WHITE*("20")$
$HRULE$
some more lines
yet another
$WHITE*("5")$

$WHITE*("20")$
$HRULE$
THE END

the script:

#!/usr/bin/env python

## Use these 3 lines for python version < 2.5
#fd=open('input')
#text=fd.read()
#fd.close()

## Use these 2 lines for python version >= 2.5
with open('input') as fd:
    text=fd.read()

old="""$WHITE*("5")$

$WHITE*("20")$
$HRULE$
"""

new="""$WHITE*("20")$
$HRULE$
$WHITE*("10")$
"""

print text.replace(old,new)

output:

first line
second line
3rd line
$WHITE*("20")$
$HRULE$
$WHITE*("10")$
some more lines
yet another
$WHITE*("20")$
$HRULE$
$WHITE*("10")$
THE END
一笑百媚生 2024-12-26 00:47:03

尝试像

 sed -e '${p;};/$WHITE\*("5")\$/,/$HRULE\$/{H;/$HRULE\$/{g;s/$HRULE\$//;s/20/10/;s/5/20/;s/\n/&$HRULE$/2p;s/.*//p;x;d;};d;};' white.txt

Crude 这样的东西,但它应该有效。

Try something like

 sed -e '${p;};/$WHITE\*("5")\$/,/$HRULE\$/{H;/$HRULE\$/{g;s/$HRULE\$//;s/20/10/;s/5/20/;s/\n/&$HRULE$/2p;s/.*//p;x;d;};d;};' white.txt

Crude, but it should work.

许你一世情深 2024-12-26 00:47:03

这可能对您有用:

 sed '/^\$WHITE\*(\"5\")\$/{N;N;N;s/.*\n\n\(\(\$WHITE\*(\"\)20\(\")\$\s*\)\n\$HRULE\$\s*$\)/\1\n\210\3/}' file

说明:

匹配第一个字符串 $WHITE*("5")$,读取接下来的 3 行并匹配其余部分。使用分组和反向引用来制定输出行。

This might work for you:

 sed '/^\$WHITE\*(\"5\")\$/{N;N;N;s/.*\n\n\(\(\$WHITE\*(\"\)20\(\")\$\s*\)\n\$HRULE\$\s*$\)/\1\n\210\3/}' file

Explanation:

Match on first string $WHITE*("5")$, read the next 3 lines and match on remainder. Use grouping and back references to formulate output lines.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文