linux - Regex to add a character at the beginning of a particular line in a file -
i have file having n number of lines want find 1 line , edit without printing file contents on screen. file dynamically created can't count spaces , all. want use regex this.
my file is:
password requisite pam_pwquality.so retry=3 password requisite pam_passwdqc.so password requisite pam_deny.so
and want make as:
#password requisite pam_pwquality.so retry=3 password requisite pam_passwdqc.so password requisite pam_deny.so
i tried these:
sed 's/password[ \t]+requisite[ \t]+pam_pwquality.so/s/^/#/' test1 x='/password[ \t]+requisite[ \t]+pam_pwquality.so/' sed -i -e "s/\($x\)/#\1/" test1 re="^[password][[ :blank: ]]*[requisite][[ :blank:]]*[pam_pwquality.so][[ :blank:]]*[retry=3]"
but no changes in file.
i use awk
:
awk '$1=="password" && $2=="requisite" && $3=="pam_deny.so" { $0="#"$0 }1' file
awk
splits line fields separated 1 or more whitespace characters (which includes tabs). makes simple check content of individual fields.
with gawk
can change file in place:
gawk -i inplace '$1=="password" && $2=="requisite" && $3=="pam_deny.so" { $0="#"$0 }1' file
Comments
Post a Comment