grep 命令替换 Fedora 上 httpd.conf 文件中的多行文本

发布于 2024-07-17 00:41:40 字数 1144 浏览 14 评论 0原文

我也是 shell 脚本和 Linux 环境的新手。

在我的项目中,我尝试从 httpd.conf 文件中搜索以下文本

<Directory '/somedir/someinnerdir'>
AllowOverride All
</Directory>

,然后删除该文本并再次重写相同的文本。 进行此重写的原因是,该脚本将在首次安装 Web 应用程序时运行,但一段时间后可能会再次运行,因为该 shell 脚本的其他部分也在执行其他任务。 因此,第一次不会找到该文本,只会简单地写入该文本,但稍后当脚本运行时,将再次找到该文本,并且需要将其删除并再次写入。

因此,我试图实现此目的的脚本部分类似于:

grep -ve "<Directory '/somedir/someinnerdir'>\\nAllowOverride All\\n</Directory>" /etc/httpd/conf/httpd.conf > tmp_direct

echo -e "<Directory '/somedir/someinnerdir'>\\nAllowOverride All\\n</Directory>" >> tmp_direct

mv tmp_direct /etc/httpd/conf/httpd.conf

我当前面前没有代码,因此上面可能存在一些语法错误,但逻辑/编码是相同的。

上面的代码片段无法实现我想要实现的目标,因为 grep 命令不支持多行搜索。

我的操作系统是 Fedora 8。

您能否在此代码中建议一些内容来实现所需的功能,或者可能是其他替代方案。

在这方面的任何帮助将受到高度赞赏。

提前致谢。


感谢您的回复。

对之前的错误代码表示歉意。 现在已更正。

Charlie 和 i-moan,由于可操作性限制,我无法实现 sed 或 perl,因为需要将其添加到我们将分发此项目的环境中。

Steve,我想检查多行。 我没有将其放入代码块中,因此它删除了目录标签。 :(

所以我需要找到其他出路。

再次感谢。

最诚挚的问候。

I am a newbie to shell scripting and to Linux environment as well.

In my project I am trying to search for following text from the httpd.conf file

<Directory '/somedir/someinnerdir'>
AllowOverride All
</Directory>

and then remove this text and again rewrite the same text.
The reason to do this rewriting is that the script will be run on first installation of the web app, but it may again be run some time later as other part of this shell script is performing other tasks as well. So for first time this text wont be found and will simply be written but later again when script is run this text will be found and will need to be removed and the written again.

So the part of my script with which I am trying to achieve this is something like :

grep -ve "<Directory '/somedir/someinnerdir'>\\nAllowOverride All\\n</Directory>" /etc/httpd/conf/httpd.conf > tmp_direct

echo -e "<Directory '/somedir/someinnerdir'>\\nAllowOverride All\\n</Directory>" >> tmp_direct

mv tmp_direct /etc/httpd/conf/httpd.conf

I dont have the code in front of me currently so there may be some syntactical errors above but the logic/coding is same.

Above code fragment is not able to do what I want to achieve as the grep command doesnt support multiline searching.

My OS is Fedora 8.

Can you please suggest something in this code to achieve what is needed or may be some other alternative.

Any help in this regard will be highly appreciated.

Thanks in advance.


Thanks for your replies.

Sorry for the previous bad code. Its corrected now.

Charlie and i-moan, due to workability constraints I wont be able to implement sed or perl as it will need to be added to the environment we will distribute this project in.

Steve, I want to do the check for multiple lines. I didnt put it in code blocks so it removed the directory tags. :(

So I will need to find some other way out.

Thanks again.

Best regards.

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

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

发布评论

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

评论(3

失退 2024-07-24 00:41:40

这听起来像是 sed(1) 的工作。 你想要的将非常接近

sed -i.bak -e '/AllowOverride All/i# '  file

注释掉该行(记住httpd.conf有注释),然后

sed -i.bak -e 's/# AllowOverride All/AllowOverride All '  file

将其拉回来。

警告我还没有尝试过,您想阅读手册页并自行测试。

This sounds like a job for sed(1). What you want will be very close to

sed -i.bak -e '/AllowOverride All/i# '  file

to comment out the line (remember httpd.conf has comments) followed by

sed -i.bak -e 's/# AllowOverride All/AllowOverride All '  file

to pujt it back.

WARNING I haven't tried it, you want to read the man page and test it youself.

゛清羽墨安 2024-07-24 00:41:40

如果你想确保匹配整行,你可以这样做:
grep -v "^AllowOverride All$"

^ 匹配行的开头,$ 匹配行的结尾。

这有帮助吗? 我不太清楚你想用 grep 做什么。

If you want to make sure you're matching an entire line, you could do:
grep -v "^AllowOverride All$"

The ^ matches the start of line and $ matches the end of line.

Does that help? I'm not exactly clear on what you're trying to do with the grep.

溺ぐ爱和你が 2024-07-24 00:41:40

我不确定我是否理解这个问题。
如果我错了,请纠正我,但您尝试在第一次运行脚本时将“AllowOverride All”添加到 httpd.conf 中。 如果是这样,您希望启用它吗?

然后在第二遍时您可能想要启用/禁用该命令?

如果我没记错的话,“AllowOverride”只允许在 < Directory > 部分因此只有在 中添加/启用才有意义。 目录>> /Directory > 定义:

如果上述为真,您可以在 perl 中执行此操作。

第一次运行:添加

perl -0777 -pi
-e 's{(.?(?!AllowOverride\s+All).?)}[\nAllowOverride
全部\n$1]ixgs 的 httpd.conf

仅当目录部分尚不包含“AllowOverride All”时,才会将其添加到其中。

第二次运行:改变

在第二次运行时,您可以按照查理的建议注释掉..或者如果您喜欢perl

perl -0777 -pi
-e's{(.?)(AllowOverride\s+All)(.?)}[$1#$2$3]ixgs' httpd.conf

评论

perl -0777 -pi
-e's{(.?)(#AllowOverride\s+All)(.?)}[$1AllowOverride\s+All$3]ixgs'
httpd.conf

正则表达式 s{}[] 非常冗长,并且有更好的正则表达式可以做同样的事情,但这对于初学者来说更容易(希望)理解:)。

perl 命令选项说明

-0777 强制 Perl 一次性读取整个文件,因为 0777 不是合法的字符值

-p 位置围绕脚本的循环

-i 允许您就地编辑文件(因此“-e”中的命令会更改文件)

-e 允许您指定一行命令行上的代码

在那一行中我们有一个正则表达式。 有关详细信息,请参阅 perldoc perlre

希望有帮助

I'm unsure I understand the question.
Correct me if I'm wrong but your trying to add 'AllowOverride All' to the httpd.conf on the 1st running of your script. If so do you want it enabled?

Then on a 2nd pass you may want to enable / disable the command?

If I remember correctly 'AllowOverride' is only allowed in a < Directory > section So it only makes sense to add / enable in < Directory >< /Directory > definition:

If above is true you could do this in perl..

1st run: to add

perl -0777 -pi
-e 's{(.?(?!AllowOverride\s+All).?)}[\nAllowOverride
All\n$1]ixgs' httpd.conf

This will add 'AllowOverride All' to a Directory section only if it does not already contain one.

2nd run: to alter

On the 2nd run you could comment out as Charlie's suggested.. or if you like perl

perl -0777 -pi
-e's{(.?)(AllowOverride\s+All)(.?)}[$1#$2$3]ixgs' httpd.conf

and to comment in

perl -0777 -pi
-e's{(.?)(#AllowOverride\s+All)(.?)}[$1AllowOverride\s+All$3]ixgs'
httpd.conf

The regex s{}[] is quite verbose and there are better regex's to do the same thing but this is easier ( hopefully ) for a beginner to understand :).

Explanation of the perl command options

-0777 force Perl to read the whole file in one shot because 0777 is not a legal character value

-p places a loop around your script

-i lets you edit files in-place ( so your command in '-e' change the file )

-e lets you specify a single line of code on the command line

In that single line we have a regex. see perldoc perlre for info on that.

Hope that helps

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