首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在bash中查找和替换文件或字符串

在bash中查找和替换文件或字符串
EN

Stack Overflow用户
提问于 2016-07-08 16:18:43
回答 1查看 99关注 0票数 0

我有这个文件httpd.conf,我想找到的是EnableSendfile,而不是包含该字符串的行,但是行的开头是"#“(而不是注释行),如果找到它,我想将它的值重置为off。

我已经试过想办法了。(我是巴什语的新手)但它不起作用。

我想使用搜索和替换命令中的变量,因为稍后我将使用数组项来循环键及其值,因此我需要使用变量而不是普通的字符串。

谢谢你的帮助,提前!

代码语言:javascript
复制
#!/bin/bash
SEARCH="EnableSendfile"
FILEPATH="/root/scripts/httpd.conf"
REPLACE="off"

if grep -Fq $SEARCH $FILEPATH
then
    line_string=`sed -n '/^EnableSendfile/p' $FILEPATH`
    result_string="${line_string/(^[^#]\w*)(?:\s(\w+))?$/$REPLACE}"
    echo $result_string
else
    echo "not found" 
fi
代码语言:javascript
复制
#
# EnableMMAP and EnableSendfile: On systems that support it, 
# memory-mapping or the sendfile syscall may be used to deliver
# files.  This usually improves server performance, but must
# be turned off when serving from networked-mounted 
# filesystems or if support for these functions is otherwise
# broken on your system.
# Defaults if commented: EnableMMAP On, EnableSendfile Off
#
#EnableMMAP off
EnableSendfile on
#EnableSendfile on

# Supplemental configuration
#
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-08 16:24:14

使用sed

代码语言:javascript
复制
sed 's/^EnableSendfile on/EnableSendfile off/'

^特殊字符在行的开头匹配,因此命令不会更改以#开头的行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38271328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档