Perl replace从脚本生成空文件,而不是从bash生成

时间:2021-04-27 15:42:40

I'm getting pretty frustrated with this problem at the moment. I can't see what I'm doing wrong. I have this problem with google chrome that it gives a notice of not being shut down properly. I want to get rid of this. Also I have some older replaces that have to do with full screen size. In bash, all the lines produce the expected result; however, in a script file, it produces an empty settings file...

我此刻对此问题感到非常沮丧。我看不出我做错了什么。我有谷歌Chrome的这个问题,它给出了没有正确关闭的通知。我想摆脱这个。此外,我还有一些与全屏尺寸有关的旧版更换。在bash中,所有行都产生预期的结果;但是,在脚本文件中,它会生成一个空的设置文件...

These lines are in the file:

这些行在文件中:

cat ~/.config/google-chrome/Default/Preferences | perl -pe "s/\"work_area_bottom.*/\"work_area_bottom\": $(xrandr | grep \* | cut -d' ' -f4 | cut -d'x' -f2),/" > ~/.config/google-chrome/Default/Preferences
cat ~/.config/google-chrome/Default/Preferences | perl -pe "s/\"bottom.*/\"bottom\": $(xrandr | grep \* | cut -d' ' -f4 | cut -d'x' -f2),/" > ~/.config/google-chrome/Default/Preferences
cat ~/.config/google-chrome/Default/Preferences | perl -pe "s/\"work_area_right.*/\"work_area_right\": $(xrandr | grep \* | cut -d' ' -f4 | cut -d'x' -f1),/" > ~/.config/google-chrome/Default/Preferences
cat ~/.config/google-chrome/Default/Preferences | perl -pe "s/\"right.*/\"right\": $(xrandr | grep \* | cut -d' ' -f4 | cut -d'x' -f1),/" > ~/.config/google-chrome/Default/Preferences
cat ~/.config/google-chrome/Default/Preferences | perl -pe "s/\"exit_type.*/\"exit_type\": \"Normal\",/" > ~/.config/google-chrome/Default/Preferences
cat ~/.config/google-chrome/Default/Preferences | perl -pe "s/\"exited_cleanly.*/\"exited_cleanly\": true,/" > ~/.config/google-chrome/Default/Preferences

I've been googling a lot for this issue; however, I do not get the right search words to get a helpful result.

我一直在谷歌上搜索这个问题;但是,我没有得到正确的搜索词来获得有用的结果。

Problem is solved by using the perl -p -i -e option like so:

通过使用perl -p -i -e选项解决问题,如下所示:

perl -p -i -e "s/\"exit_type.*/\"exit_type\": \"Normal\",/" ~/.config/google_chrome/Default/Preferences

The above line is enough to get rid of the Google chrome message of incorrect shutdown

以上行足以摆脱不正确关机的Google Chrome消息

2 个解决方案

#1


Your problem is almost certainly:

你的问题几乎可以肯定:

 > ~/.config/google-chrome/Default/Preferences

Because that > says 'truncate the file'. And worse - it does this first before starting to read it. So you truncate the file before reading it, resulting in a zero length file feeding into a zero length file.

因为那>说'截断文件'。更糟糕的是 - 它在开始阅读之前首先执行此操作。因此,在读取文件之前截断文件,导致零长度文件进入零长度文件。

I would suggest you want to do this exclusively in perl, rather than a halfway house. perl supports the -i option for an "in place edit".

我建议你只想在perl中这样做,而不是在中途宿舍。 perl支持-in选项进行“就地编辑”。

Or just write your script in perl to start with. (If give sample input and output, knocking up an example that'll do what you want will be quite straightforward).

或者只需在perl中编写脚本即可。 (如果给出样本输入和输出,那么敲一个能做你想做的事的例子就会非常简单)。

#2


If you need search and replace some text I suggest use:

如果您需要搜索并替换一些文本我建议使用:

ack -l --print0 '2011...' ~/.config/google-chrome/Default/Preferences | xargs -0 -n 1 sed -i -e 's/2011../2015.../g'

#1


Your problem is almost certainly:

你的问题几乎可以肯定:

 > ~/.config/google-chrome/Default/Preferences

Because that > says 'truncate the file'. And worse - it does this first before starting to read it. So you truncate the file before reading it, resulting in a zero length file feeding into a zero length file.

因为那>说'截断文件'。更糟糕的是 - 它在开始阅读之前首先执行此操作。因此,在读取文件之前截断文件,导致零长度文件进入零长度文件。

I would suggest you want to do this exclusively in perl, rather than a halfway house. perl supports the -i option for an "in place edit".

我建议你只想在perl中这样做,而不是在中途宿舍。 perl支持-in选项进行“就地编辑”。

Or just write your script in perl to start with. (If give sample input and output, knocking up an example that'll do what you want will be quite straightforward).

或者只需在perl中编写脚本即可。 (如果给出样本输入和输出,那么敲一个能做你想做的事的例子就会非常简单)。

#2


If you need search and replace some text I suggest use:

如果您需要搜索并替换一些文本我建议使用:

ack -l --print0 '2011...' ~/.config/google-chrome/Default/Preferences | xargs -0 -n 1 sed -i -e 's/2011../2015.../g'