sed - Sed 搜索条件 - 一个正数和一个负数,但用于精确字符串
问题描述
文件中有一段文字:
1. # OPTIONS="blah --group http blah-blah-blah"
2. OPTIONS="--group http blah-blah-blah --group1 admins"
3. OPTIONS="--group http blah-blah-blah --group1 users2"
4. OPTIONS="--group http blah-blah-blah --group1 user"
5. OPTIONS=" blah-blah-blah --group bind"
6. OPTIONS="blah-blah-blah --group radius --group1 users"
7. OPTIONS "blah-blah-blah --group http --group1 users"
8. OPTIONS="blah-blah-blah --group1 users --group http"
我需要替换--group
from http
toradius
但仅在行中,满足条件:
OPTIONS=*
* 行必须以(尤其是非注释行)开头,*--group1
不是在哪里users*
,- 当然如果
*--group
是http*
。
即,有2个搜索条件和1个替换条件。
我使用 sed 执行以下操作:
sed -i -r "/(^OPTIONS=.)(.*--group1 [^users].*)/ s/--group http/--group $radius/g" file
在这种情况下,第二个条件是否定的:要么u
or要么s
or e
or r
or s , but I need to get negative for the whole exact string
users`。
从下面的示例中,只有第 2、3、4 行满足条件。怎么会达到这样?
解决方案
sed
有它自己的语言和它自己的 simple if
s。不要写一个表达式。实际写下条件:
sed '
# the line must begginning from OPTIONS= (in particular it is non commented line),
/^OPTIONS=/{
# where --group1 is NOT users,
/--group1 users/!{
# of course if --group is http.
/--group http/{
s//--group radius/
}
}
}'
推荐阅读
- python-3.x - 为什么我的龟窗会立即关闭?
- python - ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 13 is different from 1)
- libcurl - 如何使用 libcurl 获取 ftp 服务器上的文件大小?
- r - 尝试使用 R 进行异常检测时遇到错误
- mysql - Mysql - 在一个范围内的子集查询的总和
- java - Recyclerview 没有更新数据集,仍然显示旧值
- postgresql - 使 postgreSQL/SQL Alchemy orm 选择更快
- c# - Assemblie.load.getType 不适用于类型?
- machine-learning - 为什么precision_recall_fscore_support 返回2 个值?
- android - java.lang.IllegalArgumentException:没有为片段 DiscDispFragment 找到 id 0x184f(未知)的视图