Using sed to replace a word at specific location


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using sed to replace a word at specific location
# 1  
Old 01-17-2013
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
Code:
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

Last edited by Scott; 01-18-2013 at 03:09 AM.. Reason: Code tags
# 2  
Old 01-18-2013
Code:
awk '$1=="ssh"||$1=="emagent"{sub("prohibit","aix",$NF);}1' filename

# 3  
Old 01-18-2013
Hi,

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

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


Last edited by Scott; 01-18-2013 at 03:10 AM.. Reason: Code tags
# 4  
Old 01-18-2013
I tried using both awk and sed as suggested above but non of them have resolved the issue Smilie

Code:
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 Smilie


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

Last edited by Scott; 01-18-2013 at 03:11 AM.. Reason: Code tags
# 5  
Old 01-18-2013
What is the output with sed cammand???

Please take a backup of /etc/pam.conf

try this once...

Code:
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.

Last edited by Scott; 01-18-2013 at 03:11 AM.. Reason: Code tags
# 6  
Old 01-18-2013
Code:
# ./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.

Last edited by Scott; 01-18-2013 at 03:12 AM.. Reason: Code tags
# 7  
Old 01-18-2013
Quote:
Originally Posted by srinivas matta
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.
I have tried the above command with your file data only. its working fine for me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy files based on specific word in a file name & its extension and putting it in required location

Hello All, Since i'm relatively new in shell script need your guidance. I'm copying files manually based on a specific word in a file name and its extension and then moving it into some destination folder. so if filename contains hyr word and it has .md and .db extension; it will move to TUM/HYR... (13 Replies)
Discussion started by: prajaktaraut
13 Replies

2. Shell Programming and Scripting

Deleting lines in a fixed length file where there is a word at specific location

I have a big file having 100 K lines. I have to read each line and see at 356 character position whethere there is a word "W" in it. If it is their then don't delete the line otherwise delete it. There are two lines as one Header and one trailer which should remain same. Can somebody... (5 Replies)
Discussion started by: mohit kanoongo
5 Replies

3. Shell Programming and Scripting

How to find a word and move it a specific location in xml file using perl?

Hi friends, I have one XML file having below structure :- INput XML file :- <?xml version="1.0" encoding="UTF-8"?> <START> <A=value1> <attr name1="a1"> </A> <B=value2> <attr name2="b1"> <attr name3="c1"> </B> </START> output xml file should be === (3 Replies)
Discussion started by: harpal singh
3 Replies

4. Shell Programming and Scripting

Replace spaces at a specific Location

Hello All, I have a comma separated file which needs to be loaded to the database. But, I need to trim the white spaces for a specific column before its loaded. Below is the sample line from the input file: 690,690,0575,"01011940","01011940", , , , , ,36720,36722,V2020,V2999,... (6 Replies)
Discussion started by: Praveenkulkarni
6 Replies

5. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

6. Shell Programming and Scripting

Help with replace character based on specific location

Hi, I got long list of reference file (column one is refer to the header in input file; column 2 is info of start position in input file; column 3 is info of end position in input file;) shown as below: read_2 10 15 read_3 5 8 read_1 4 10 . . . Input file (huge file with total... (6 Replies)
Discussion started by: perl_beginner
6 Replies

7. Shell Programming and Scripting

Find and replace a string a specific value in specific location in AIX

Hi, I have following samp.txt file in unix. samp.txt 01Roy2D3M000000 02Rad2D3M222222 . . . . 10Mik0A2M343443 Desired Output 01Roy2A3M000000 02Rad2A3M222222 . . (5 Replies)
Discussion started by: techmoris
5 Replies

8. Shell Programming and Scripting

Finding a word at specific location in a string

Hi All , I have different strings (SQL queries infact) of different lengths such as: 1. "SELECT XYZ FROM ABC WHERE ABC.DEF='123' " 2. "DELETE FROM ABC WHERE ABC.DEF='567'" 3. "SELECT * FROM ABC" I need to find out the word coming after the... (1 Reply)
Discussion started by: swapnil.nawale
1 Replies

9. Shell Programming and Scripting

how to Add word at specific location in line

eg . i have file x.txt contains : java coding , shell scriptting etc... now i want to add "is langauge" after word java. output should be java is langauge coding , shell scriptting etc... any idea how to use shell script to do it ? (10 Replies)
Discussion started by: crackthehit007
10 Replies

10. Shell Programming and Scripting

How to replace a specific word in specific column?

Hi My orginal file is like (100s of lines) id host ip location remarks 1 host1 ip1 - xxx 2 host2 ip2 - xxx 3 host3 ip3 - xxx -- -- 9 host9 ip9 - xxx I have a ref file like host1 location1 host2 location2 host3 location3 -- --... (6 Replies)
Discussion started by: ./hari.sh
6 Replies
Login or Register to Ask a Question