bash 简单的查找并替换为 sed

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12735788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:47:16  来源:igfitidea点击:

Simple find and replace with sed

bash

提问by Crystal

I'm trying to replace a number that is in a couple different strings in a text file. Basically it would take the form of

我正在尝试替换文本文件中几个不同字符串中的数字。基本上它会采用以下形式

tableNameNUMBER
carNUMBER

I'm pretty new to bash and scripting and I wasn't sure how to replace NUMBERwith what I pass in. So I've tried this:

我对 bash 和脚本很陌生,我不确定如何NUMBER用我传入的内容替换。所以我试过这个:

#! /usr/bin/env bash
sed "s/NUMBER//" myScript.txt > test.txt

then at the command line:

然后在命令行:

sh test.sh 123456

This only works if NUMBERis on its own, without tableNameor carpreceding it. How can I replace NUMBERin those cases. Is it better to have ${NUMBER}? Sorry if these are totally noob questions.

这仅NUMBER在单独使用时才有效,没有tableName或在它car之前。NUMBER在这些情况下我该如何更换。有更好${NUMBER}吗?对不起,如果这些完全是菜鸟问题。

采纳答案by David W.

This should work just fine:

这应该可以正常工作:

sed "s/NUMBER//g" myScript.txt > test.txt

The gon the end allows set to replace NUMBER if it appears multiple times on one line.

g对端允许设置,如果它出现多次在一行上,以取代NUMBER。

In fact, a quick test:

事实上,一个快速的测试:

foo.txt

foo.txt

carNUMBER
tableNameNUMBER
NUMBER
NUMBERfoo

$ NUMBER=3.14
$ sed "s/NUMBER/$NUMBER/g" foo.txt
car3.14
tableNumber3.14
3.14
3.14foo

Isn't that what your sedcommand is doing?

这不是你的sed命令吗?

If you want to make sure you don'tchange NUMBER unless it's by itself, use \baround NUMBER:

如果您想确保更改 NUMBER 除非它本身,请使用\baround NUMBER

$ sed "s/\bNUMBER\b/$NUMBER/g" foo.txt
carNumber
tabelNumberNUMBER
3.14
NUMBERfoo

If you don't care about the case of the string NUMBER, put an ion the end of the sed command:

如果您不关心 string 的大小写NUMBER,请将 ani放在 sed 命令的末尾:

$ sed "s/NUMBER/$NUMBER/gi" foo.txt

回答by antak

Since you've stated you're new to this, I'm going to first answer some problems that you haven't asked.

既然你已经说过你是新手,我将首先回答一些你没有问过的问题。

Your shebangis wrong

你的shebang错了

The first bytes of your script should be #!(a hash and an exclamation mark; referred to as a shebang). You have #1(hash-one), which is gibberish. This shebang line tells the system to use bashto interpret your file (i.e. your script) when executed. (More or less. Technically speaking, it's actually given to env, which finds bashbefore handing it over.)

脚本的第一个字节应该是#!(散列和感叹号;称为shebang)。你有#1(哈希一),这是胡言乱语。这个shebang行告诉系统bash在执行时用来解释你的文件(即你的脚本)。(或多或少。从技术上讲,它实际上是给 的env,它bash在移交之前找到了。)

Once you've fixed up the shebang, set the permissions on the script to let the system know it's executable:

一旦你修复了shebang,在脚本上设置权限让系统知道它是可执行的:

$ chmod a+x test.sh

Then run it like so:

然后像这样运行它:

$ ./test.sh 123456

As @gokcehan also commented, running your script with sh ...when you have a shebang is redundant, and isn't preferable for other reasons.

正如@gokcehan 也评论的那样,sh ...当你有一个shebang 时运行你的脚本是多余的,并且由于其他原因而不是可取的。



As for what you were asking, you can easily test your regex replacement:

至于你问什么,你可以很容易地测试你的正则表达式替换:

$ echo tableNameNUMBER | sed "s/NUMBER/123456/"
tableName123456

And it seems to work just fine.

它似乎工作得很好。

Note: The preceding $merely denotes that I typed it into my console and isn't part of the actual command.

注意:前面$仅表示我将它输入到我的控制台中,而不是实际命令的一部分。

回答by Piotr Wadas

to replace 1111 to 2222 you should probably try

要将 1111 替换为 2222,您可能应该尝试

cat textfile.txt | sed -e 's/1111/2222/g'

or

或者

cat textfile.txt | sed -e 's/1111/2222/g' > output.txt

or

或者

NUMBER=1111 ; NEWNUMBER=2222; cat textfile.txt | sed -e "s/$NUMBER/$NEWNUMBER/g"

There's no need to create separate script for such trivial task. Note "-e" switch for sed, "g" appended at the end of quoted command, and the difference between single and double quotes.

无需为此类微不足道的任务创建单独的脚本。注意 sed 的 "-e" 开关,在带引号的命令末尾附加 "g",以及单引号和双引号之间的区别。