如何查找和替换,跳过含有特定字符行? [英] How to find and replace, skipping lines containing a particular character?
本文介绍了如何查找和替换,跳过含有特定字符行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 SED
查找和替换模式的第一次出现在文件中:
I am using sed
to find and replace the first appearance of an pattern in a file:
sed -i "0,\#pattern#s##replacement#" ./file.txt
我怎么能告诉它跳过任何包含这些行 {
。下面是一个简单的输入:
How can I tell it to skip any of those lines containing {
. Here is a sample input:
This line does not have the item, so it is skipped.
This line has the pattern, but also has {, so it is skipped too.
This line has the pattern, and not the character, so it will be changed.
下面是输出示例:
This line does not have the item, so it is skipped.
This line has the pattern, but also has {, so it is skipped too.
This line has the replacement, and not the character, so it will be changed.
- 只有一个变化在整个文件的。
- 如果有图案的每一行还包含
{
,那么不更改该文件的。 - Only one change is made in the entire file.
- If every line with the pattern also contains
{
, then no changes are made to the file.
如何使用 SED
来查找和替换项目的首次亮相,但跳过含有另一种模式的线,在这种情况下 {
?
How can I use sed
to find and replace the first appearance of an item, but skipping any lines that contain another pattern, in this case {
?
推荐答案
这可能会为你工作(GNU SED):
This might work for you (GNU sed):
sed '/{/b;/pattern/{s//replacement/;:a;n;ba}' file
跳过含有任何行 {
上的第一个模式匹配,替换它替换然后打印其余线路的档案结尾。
Skips any lines containing {
on matching the first pattern, substitutes it for the replacement then prints remaining lines to the end-of-file.
这篇关于如何查找和替换,跳过含有特定字符行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文