Using sed to replace a word at specific location

I'm try to change a the prohibit to aix for the lines starting with ssh and emagent and rest should be the same. Can anyone please suggest me how to do that using a shell script or sed

passwd  account required        /usr/lib/security/pam_prohibit
passwd  session required        /usr/lib/security/pam_prohibit
sshd    auth    required        /usr/lib/security/pam_prohibit
sshd    account required        /usr/lib/security/pam_prohibit
ssh     auth    required        /usr/lib/security/pam_prohibit
ssh     account required        /usr/lib/security/pam_prohibit
emagent auth    required        /usr/lib/security/pam_prohibit
emagent account required        /usr/lib/security/pam_prohibit

Thank you

awk '$1=="ssh"||$1=="emagent"{sub("prohibit","aix",$NF);}1' filename

Hi,

if you are unfamilier with awk, you can use below sed command.

#search for lines start with ssh and emagent and replace prohibit with aix
sed "/ssh\|emagent/s/prohibit/aix/g" $file

I tried using both awk and sed as suggested above but non of them have resolved the issue :confused:

file="/etc/pam.conf"
sed "/ssh\|emagent/s/prohibit/aix/g" $file

cat /etc/pam.conf | \
sed "/ssh\|emagent/s/prohibit/aix/g" > /etc/pam.conf

cat /etc/pam.conf | \
sed -e "/ssh\|emagent/s/prohibit/aix/g" > /etc/pam.conf

cat /etc/pam.conf | \
sed -e '/ssh\|emagent/s/prohibit/aix/' > /etc/pam.conf

awk '$1=="ssh"||$1=="emagent"{sub("prohibit","aix",$NF);}1' /etc/pam.conf

nothing really worked :confused:

/etc/pam.conf is big file which have lot of other date too .

What is the output with sed cammand???

Please take a backup of /etc/pam.conf

try this once...

sed -i "/ssh\|emagent/s/prohibit/aix/g" /etc/pam.conf

open /etc/pam.conf file and check whether it was changed accordingly or not.

Please send me the output if you face any problem.

# ./test2.sh
sed: Not a recognized flag: i
Usage:  sed [-n] [-u] Script [File ...]
        sed [-n] [-u] [-e Script] ... [-f Script_file] ... [File ...]

Yes, I took the backup of /etc/pam.conf file. It is not allowing me to use "-i" in script.

I have tried the above command with your file data only. its working fine for me.

That is so unfortunate for me, but I tried replacing -i with -e and even trail and error with single and double quotes, it never worked. Is there any other way to solve this problem? I even tried awk as above but it didn't changed the content at all.

Are you running this in linux sever only,right?

which flavour of linux you are using. "-i" command is the well known command to me..am using this from past 3 yrs.

I'm trying that on aix. My bad, to say that so late. I thought you might know that my seeing probhit to aix.

we have some minor differences for linux to aix.

I dont have aix server now to check, please try like this hope it will work fine to you.

sed  "/ssh\|emagent/s/prohibit/aix/g" /etc/pam.conf > output_file

(at the same time try with single quotes)

---------- Post updated at 12:56 AM ---------- Previous update was at 12:51 AM ----------

I am sure single code will work in aix.

sed '/ssh\|emagent/s/prohibit/aix/g' /etc/pam.conf > output_file

Have a good time.

I'm sorry to say this, but still unable to change the content. :confused:

Please try below two commands one with single quote and another with double quote and these commands will replace prohibit with aix word for the lines which are starting with ssh or emagent.

Note :- below commands will replace the lines starting with ssh and emagent only not with sshd. if you want to replace with sshd also please remove the space after "ssh" in the command.

  sudo sed "/^ssh \|^emagent/s/prohibit/aix/g"  filename 
 sudo sed '/^ssh \|^emagent/s/prohibit/aix/g' filename 

Best Regards,
Kalyan

Thank you, it worked.

1 Like