简体   繁体   English

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

[英]Changing contents of a file through shell script

I have a requirement where in I need to change the contents of a file say xyz.cfg.我有一个要求,我需要更改文件的内容,比如 xyz.cfg。 the file contains values like:该文件包含如下值:

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.我想在需要时通过 shell 脚本更改此内容并保存文件。 Changed content can look like:更改后的内容可能如下所示:

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 脚本实现此目的?

How about something like:怎么样:

#!/bin/bash

addr=$1
port=$2
user=$3

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

Where $1,$2 and $3 are the arguments passed to the script.其中$1,$2$3是传递给脚本的参数。 Save it a file such as script.sh and make sure it executable with chmod +x script.sh then you can run it like:将它保存为一个文件,例如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.这为您提供了基本结构,但是您需要考虑验证输入等。

* *

#! /bin/sh
file=xyz.cfg
addr=$1
port=$2
username=$3
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我希望这个对初学者来说更容易理解

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.根据我的评论,下面 sudo_O 的答案正是您想要的示例。 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.我要补充的是,您通常希望对多个文件、跨越子目录等进行此类匹配,因此了解 find/xargs,您可以将两者结合起来。 A simple example of say changing the subnet in a bunch of .cfg files could be:更改一堆 .cfg 文件中的子网的一个简单示例可能是:

find -name '*.cfg' -print0 | xargs -0 -I {} sed -ie 's/\(192.168\)\.1/\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 的示例中相同)

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

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

In addition to the solutions above, you should watch out for the escape characters in the text you replacing.除了上述解决方案外,您还应该注意替换文本中的转义字符。

For example, if you replacing something like /home/user/ then you will not get the result that you would like to get.例如,如果您替换诸如/home/user/那么您将不会得到您想要的结果。

To solve this problem you can change the delimiter from / to |要解决此问题,您可以将分隔符从/更改为| . . See the code sample below.请参阅下面的代码示例。

OLD="path/to/replace"
NEW="new/path"
file=file-to-search.log

sed "s|$OLD|$NEW|g" $file

You can achieve this as follows -您可以通过以下方式实现这一点 -

File script.sh :文件script.sh

while [ $# -gt 0 ]
do
 case "$1" in
     --group-address)
            export NEW_VAL1=$2
            shift 2
            ;;
     --port)
            export NEW_VAL2=$2
            shift 2
            ;;
     --username)
            export NEW_VAL3=$2
            shift 2
            ;;

    *)
    echo "Unrecognized option: $1"
    usage 1
    esac
done


sed -i 's/group address=.*/group address='$NEW_VAL1'/g' xyz.cfg
sed -i 's/port=.*/port=.'$NEW_VAL2'/g' xyz.cfg
sed -i 's/Jboss username=.*/Jboss username='$NEW_VAL3'/g' xyz.cfg

You can now update these values by passing respective arguments -您现在可以通过传递相应的参数来更新这些值 -

  • --group-address --组地址
  • --port - 港口
  • --username - 用户名

on command line to this script在这个脚本的命令行上

For example -例如 -

./script.sh --group-address 127.8.7.7 --port  7822 --username xyz_ITR4

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

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