[英]Perl + how to add lines before word in file
I write the following code in my bash script ( runs on redhat 7.2 version ) 我在我的bash脚本中编写以下代码(在redhat 7.2版本上运行)
in order to add the content of $info variable before the word – ERROR: in file 为了在单词-ERROR: in file之前添加$ info变量的内容
export info="The Postfix error(8) delivery agent processes delivery requests from the queue manager. Each request specifies a queue file, a sender address, the reason for non-delivery (specified as the next-hop destination), and recipient"
perl -i -plne 'print "$ENV{info}" if(/ERROR:/);' file
after we run the code the output of file that we can see is too long and its better to separate the line to 3 lines 在我们运行代码之后,我们可以看到的文件输出太长,最好将行分成3行
more file
The Postfix error(8) delivery agent processes delivery requests from the queue manager. Each request specifies a queue file, a sender address, the reason for non-delivery (specified as the next-hop destination), and recipient
ERROR:
So I add “\\n” in the info line as the following: and we run the code again 所以我在信息行中添加“\\ n”如下:我们再次运行代码
export info="The Postfix error(8) delivery agent processes delivery requests from the queue manager. \nEach request specifies a queue file, a sender address, \nthe reason for non-delivery (specified as the next-hop destination), and recipient"
perl -i -plne 'print "$ENV{info}" if(/ERROR:/);' file
but the file still not include the new lines ( actually the "\\n" are in line ) 但文件仍然不包括新行(实际上“\\ n”在行中)
The Postfix error(8) delivery agent processes delivery requests from the queue manager. \nEach request specifies a queue file, a sender address, \nthe reason for non-delivery (specified as the next-hop destination), and recipient
ERROR:
while Expected results should be: 预期结果应为:
The Postfix error(8) delivery agent processes delivery requests from the queue manager.
Each request specifies a queue file, a sender address,
the reason for non-delivery (specified as the next-hop destination), and recipient
ERROR:
Where I am wrong here? 我在哪里错了?
The problem here is that in Bash double quotes don't cause \\n
to be interpreted as a newline character. 这里的问题是在Bash中双引号不会导致
\\n
被解释为换行符。
For this, I think I'd just include literal newlines in the string: 为此,我想我只是在字符串中包含文字换行符:
export info="The Postfix error(8) delivery agent processes delivery requests from the
queue manager.
Each request specifies a queue file, a sender address,
the reason for non-delivery (specified as the next-hop destination), and recipient"
perl -i -plne 'print "$ENV{info}" if(/ERROR:/);' file
Otherwise, you could use printf
, but that seems overkill: 否则,您可以使用
printf
,但这似乎有点过分:
export info=$(printf "The Postfix error(8) delivery agent processes delivery requests from the queue manager. \nEach request specifies a queue file, a sender address, \nthe reason for non-delivery (specified as the next-hop destination), and recipient")
perl -i -plne 'print "$ENV{info}" if(/ERROR:/);' file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.