Error[8]: Undefined offset: 5, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我有一个很大的apache配置文件,在每个虚拟主机部分中,我想添加自己的日志条目.我想知道我是否可以用脚本来做. 我当前的配置文件是这样的; ServerName abc.com some information. … …… 我希望有类似的东西; ServerName abc.com CustomLog "/usr/local/logs/abc.com.log" 我有一个很大的apache配置文件,在每个虚拟主机部分中,我想添加自己的日志条目.我想知道我是否可以用脚本来做.

我当前的配置文件是这样的;

Servername abc.com   some information.   …   ……

我希望有类似的东西;

Servername abc.com    CustomLog "/usr/local/logs/abc.com.log"    some information.    …   ……

有可能通过某种脚本吗?我有很多这样的虚拟主机条目,所以@R_502_6771@是不可能的..任何想法?

解决方法 awk可以更简单易用.

awk 'NR==3{print "my log"}1' input_file

> NR是一个跟踪行号的内置变量.
>您还可以使用-v和变量名来动态传递值,而不是在脚本中对其进行硬编码.例如. awk -v line =“$var”’NR == line {print“my log”} 1’input_file.在这种情况下,line是一个awk变量,$var可以是在awk范围之外定义的bash变量.

测试:

[jaypal:~/Temp] cat fileServername abc.com   some information.   …   ……  [jaypal:~/Temp] awk 'NR==3{print "my log"}1' file # add log after 2 linesServername abc.com   some information.   my log…   ……  [jaypal:~/Temp] awk 'NR==4{print "my log"}1' file # add log after 3 linesServername abc.com   some information.   …   my log……  [jaypal:~/Temp] var=2 # define a variable which holds the line number you want to print on[jaypal:~/Temp] awk -v line="$var" 'NR==line{print "my log"}1' fileServername abc.com   my logsome information.   …   ……

在评论中,我看到了从匹配模式(Servername,在此示例中)开始的3行之后添加日志的问题.为此,你可以尝试这样的事情 –

awk’/ Servername / {a = NR; pr​​int; next} NR ==(a 3){print $0; print“my log”; next} 1’file

[jaypal:~/Temp] awk '/Servername/{a=NR;print;next} NR==(a+3){print[+++];print "my log";next}1' fileServername abc.com   some information.   …   ……  my log
总结

以上是内存溢出为你收集整理的linux – 在大文件中搜索并添加模式全部内容,希望文章能够帮你解决linux – 在大文件中搜索并添加模式所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
linux – 在大文件中搜索并添加模式_系统运维_内存溢出

linux – 在大文件中搜索并添加模式

linux – 在大文件中搜索并添加模式,第1张

概述我有一个很大的apache配置文件,在每个虚拟主机部分中,我想添加自己的日志条目.我想知道我是否可以用脚本来做. 我当前的配置文件是这样的; ServerName abc.com some information. … …… 我希望有类似的东西; ServerName abc.com CustomLog "/usr/local/logs/abc.com.log" 我有一个很大的apache配置文件,在每个虚拟主机部分中,我想添加自己的日志条目.我想知道我是否可以用脚本来做.

我当前的配置文件是这样的;

Servername abc.com   some information.   …   ……

我希望有类似的东西;

Servername abc.com    CustomLog "/usr/local/logs/abc.com.log"    some information.    …   ……

有可能通过某种脚本吗?我有很多这样的虚拟主机条目,所以@R_502_6771@是不可能的..任何想法?

解决方法 awk可以更简单易用.

awk 'NR==3{print "my log"}1' input_file

> NR是一个跟踪行号的内置变量.
>您还可以使用-v和变量名来动态传递值,而不是在脚本中对其进行硬编码.例如. awk -v line =“$var”’NR == line {print“my log”} 1’input_file.在这种情况下,line是一个awk变量,$var可以是在awk范围之外定义的bash变量.

测试:

[jaypal:~/Temp] cat fileServername abc.com   some information.   …   ……  [jaypal:~/Temp] awk 'NR==3{print "my log"}1' file # add log after 2 linesServername abc.com   some information.   my log…   ……  [jaypal:~/Temp] awk 'NR==4{print "my log"}1' file # add log after 3 linesServername abc.com   some information.   …   my log……  [jaypal:~/Temp] var=2 # define a variable which holds the line number you want to print on[jaypal:~/Temp] awk -v line="$var" 'NR==line{print "my log"}1' fileServername abc.com   my logsome information.   …   ……

在评论中,我看到了从匹配模式(Servername,在此示例中)开始的3行之后添加日志的问题.为此,你可以尝试这样的事情 –

awk’/ Servername / {a = NR; pr​​int; next} NR ==(a 3){print $0; print“my log”; next} 1’file

[jaypal:~/Temp] awk '/Servername/{a=NR;print;next} NR==(a+3){print;print "my log";next}1' fileServername abc.com   some information.   …   ……  my log
总结

以上是内存溢出为你收集整理的linux – 在大文件中搜索并添加模式全部内容,希望文章能够帮你解决linux – 在大文件中搜索并添加模式所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/yw/1021924.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存