简体   繁体   English

使用 bash 增加 .txt 文件中的变量

[英]Increment a variable in a .txt file with bash

I'm trying to change a variable in a .txt file but I can't figure how.我正在尝试更改 .txt 文件中的变量,但我不知道如何更改。 I've looked on internet and tried what was written but it doesn't work.我查看了互联网并尝试了所写的内容,但它不起作用。 I get the following error message :我收到以下错误消息:

sed: 1: "abc.txt": command a expects \\ followed by text sed: 1: "abc.txt": 命令 a 期望 \\ 后跟文本

Here was what I originally tried to run:这是我最初尝试运行的内容:

sed -i 's/Hi/Good Morning/' abc.txt

Check your man page for sed.检查您的手册页以了解 sed。 -i probably requires a suffix argument. -i可能需要一个后缀参数。

-i extension -i 扩展名

Edit files in-place, saving backups with the specified extension.就地编辑文件,使用指定的扩展名保存备份。 If a zero-length extension is given, no backup will be saved.如果给出零长度扩展,则不会保存任何备份。 It is not recommended to give a zero-length extension when in-place editing files, as you risk corruption or partial content in situations where disk space is exhausted, etc.不建议在就地编辑文件时提供零长度扩展名,因为在磁盘空间耗尽等情况下,您可能会面临损坏或部分内容的风险。

What's happening is that sed is taking 's/Hi/Good Morning/' to be the argument to -i , and then considering abc.txt to be the sed command itself.发生的事情是 sed 将's/Hi/Good Morning/'作为-i的参数,然后将abc.txt视为 sed 命令本身。

By adding a proper backup extension to the command, it will work like so:通过向命令添加适当的备份扩展,它将像这样工作:

$ cat abc.txt
Hi, How are you

$ sed -i .bak 's/Hi/Good Morning/' abc.txt

$ cat abc.txt
Good Morning, How are you

$ cat abc.txt.bak
Hi, How are you

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM