Linux 通过 shell 脚本更改文件的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14643531/
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-08-06 18:53:48  来源:igfitidea点击:

Changing contents of a file through shell script

linuxbashshell

提问by user2031888

I have a requirement where in I need to change the contents of a file say xyz.cfg. the file contains values like:

我有一个要求,我需要更改文件的内容,比如 xyz.cfg。该文件包含如下值:

group address=127.8.8.8
port=7845
Jboss username=xyz_ITR3

I want to change this content when ever needed through a shell script and save the file. Changed content can look like:

我想在需要时通过 shell 脚本更改此内容并保存文件。更改后的内容可能如下所示:

group address=127.8.7.7  
port=7822
Jboss username=xyz_ITR4

How can i achieve this using shell script by taking user input or otherwise?

如何通过获取用户输入或其他方式使用 shell 脚本实现此目的?

采纳答案by Chris Seymour

How about something like:

怎么样:

#!/bin/bash

addr=
port=
user=

sed -i -e "s/\(address=\).*//" \
-e "s/\(port=\).*//" \
-e "s/\(username=\).*//" xyz.cfg

Where $1,$2and $3are the arguments passed to the script. Save it a file such as script.shand make sure it executable with chmod +x script.shthen you can run it like:

传递给脚本的参数在哪里$1,$2$3在哪里。将其保存为一个文件,例如script.sh并确保它可执行,chmod +x script.sh然后您可以像这样运行它:

$ ./script.sh 127.8.7.7 7822 xyz_ITR4

$ cat xyz.cfg
group address=127.8.7.7
port=7822
Jboss username=xyz_ITR4

This gives you the basic structure however you would want to think about validating input ect.

这为您提供了基本结构,但是您需要考虑验证输入等。

回答by herdingofthecats

sed -i 's/something/other/g' filename.txt 

Will edit filename.txt in-place, and change the word 'something' to 'other'

将就地编辑 filename.txt,并将单词“something”更改为“other”

I think -i may be a GNU extension though, but if it's OK for you, you can add it via find, xargs etc.

我认为 -i 可能是一个 GNU 扩展,但如果它适合你,你可以通过 find、xargs 等添加它。

If you would like to change it in a shell script, you can take arguments on the command-line and refer to them by number, eg $1

如果您想在 shell 脚本中更改它,您可以在命令行中获取参数并通过数字引用它们,例如 $1

Edit:

编辑:

As per my comment, sudo_O's answer below is exactly the example that you want. What I will add is that it's common that you'll want to do such matches with multiple files, spanning subdirectories etc, so get to know find/xargs, and you can combine the two. A simple example of say changing the subnet in a bunch of .cfg files could be:

根据我的评论,下面 sudo_O 的答案正是您想要的示例。我要补充的是,您通常希望对多个文件、跨越子目录等进行此类匹配,因此了解 find/xargs,您可以将两者结合起来。更改一堆 .cfg 文件中的子网的一个简单示例可能是:

find -name '*.cfg' -print0 | xargs -0 -I {} sed -ie 's/\(192.168\)\.1/\.7/' {}

Note the -print0/-0 args to find/xargs (very useful for paths/filenames with spaces), and that you have to escape the capturing brackets because of the shell (same in sudo's example)

请注意 find/xargs 的 -print0/-0 args(对于带有空格的路径/文件名非常有用),并且由于 shell,您必须转义捕获括号(与 sudo 的示例中相同)

回答by Yuriy

*

*

#! /bin/sh
file=xyz.cfg
addr=
port=
username=
sed -i 's/address=.*/address='$addr'/' $file
sed -i 's/port=.*/port='$port'/' $file
sed -i 's/username=.*/username='$username'/' $file

*

*

I hope this one will be simpler to understand for beginners

我希望这个对初学者来说更容易理解

回答by kiran jadhav

sed -i "s/$name/$new_name/g" address.txt

This command can also be used for modifying the data.

该命令也可用于修改数据。