简体   繁体   中英

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 requisite 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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