简体   繁体   English

如何替换bash脚本中文件的特定行?

[英]How can I replace a specific line of a file in bash script?

I have a file inside the one directory, I want to replace one specific line with another line. 我在一个目录中有一个文件,我想用另一行替换一个特定的行。

In my case the line is - "password requisite pam_pwquality.so" and I want to make it as 在我的情况下,该行是-“ password Requirement pam_pwquality.so”,我想将其设置为

"#password requisite pam_pwquality.so". “ #password必需的pam_pwquality.so”。

I tried like this: 我这样尝试过:

 cat test1 | while read line
    do
       if grep -q "$str" test1; then
            echo "found"
            sed -i -e "s/${str}/${str1}/g" test1

       fi

but it is showing multiple found statements but I am having only one copy of this line. 但是它显示了多个找到的语句,但是我只有这一行的一个副本。

$ x='password requisite pam_pwquality.so'
$ sed -i "s/\($x\)/#\1/" file

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

相关问题 如何在 bash 命令行或脚本中位置更改的文件中替换特定字符? - How can I replace a specific character in a file where it's position changes in bash command line or script? 如何使用 bash 脚本在文件中的特定单词或行后添加一行? - How can i add a block of line after a specific word or line in a file using bash script? 如何在bash中替换文件中的特定行出现 - How to replace the specific line occurence in file in bash 我如何创建Linux脚本(bash,sed,awk…)在配置文件的特定部分添加一行 - How can i create a Linux Script (bash, sed, awk…) to add a line in a specific section of a configuration's file 如何使用行号 bash 脚本将文件中的特定行替换为字符串变量? - How to replace a specific line in a file with a string variable using the line number bash script? Bash:如何在文件中的特定位置插入一行? - Bash: How can I insert a line to a specific location within a file? 如何替换bash脚本 - how to replace line bash script 如何替换 unix 文件中特定行的单词 - How can I replace a word at a specific line in a file in unix 我如何编写bash脚本,该脚本从文件中返回从头开始直到特定符号的行? - How can I write bash script that returns line from file which starts from the begining untill specific symbol? Bash:如何在osx bash中用换行替换字符串? - Bash: How can I replace a string by new line in osx bash?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM