Search and replace in shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and replace in shell scripting
# 1  
Old 06-20-2017
Search and replace in shell scripting

I am trying to write shell script to find and replace using Sed. but i am unable to complete the setting. need help in doing that.

Requirement:

FROM
Code:
"${O_INSTANCE}/diag/logs/${C_TYPE}/${C_NAME}/httpd.pid"

TO
Code:
"/var/opt/<SID>_<HOSTNAME>/Apache/httpd.pid"


Last edited by avmk0407; 06-20-2017 at 07:43 PM.. Reason: Please use code tags
# 2  
Old 06-20-2017
Quote:
Originally Posted by avmk0407
I am trying to write shell script to find and replace using Sed. but i am unable to complete the setting. need help in doing that.

Requirement:

FROM
Code:
PidFile "${O_INSTANCE}/diagnostics/logs/${COMPONENT_TYPE}/${COMPONENT_NAME}/httpd.pid"

TO
Code:
PidFile "/var/opt/<SID>_<HOSTNAME>/Apache/httpd.pid"

I did the code in following way.

Code:
search='PidFile "${O_INSTANCE}/diagnostics/logs/${COMPONENT_TYPE}/${COMPONENT_NAME}/httpd.pid"'
replace='PidFile "/var/opt/<SID>_<HOSTNAME>/Apache/httpd.pid"'
for file in `find -name 'httpd.conf'`; do
  grep "$search" $file &> /dev/null
  if [ $? -ne 0 ]; then
    echo "Search string not found in $file!"
  else
    sed -i 's/$search/$replace/' $file
  fi  
done

Correction:
Code:
search="PidFile ${O_INSTANCE}/diagnostics/logs/${COMPONENT_TYPE}/${COMPONENT_NAME}/httpd.pid"

In order to expand the variables in the search it cannot be surrounded by single quotes.
That's applicable for the sed command as well.
Instead of
Code:
grep "$search" $file &> /dev/null
  if [ $? -ne 0 ]; then

You could do
Code:
if grep -q "$search" "$file"; then
   ...
else
   ...
fi

# 3  
Old 06-20-2017
Update:

Code:
grep "$search" $file > /dev/null

and

Code:
sed -i 's?'"$search"'?'"$replace"'?' $file


Last edited by rdrtx1; 06-20-2017 at 07:27 PM..
This User Gave Thanks to rdrtx1 For This Post:
# 4  
Old 06-20-2017
Why not - feasability depending on the probability of files containing "$search" - just
Code:
for file in $(find -name 'httpd.conf'); do
   sed -i "s/$search/$replace/" $file
done

EDIT: You may need to replace the slash as a delimiter as suggested by rdrtx1...

Last edited by RudiC; 06-21-2017 at 01:25 AM.. Reason: Added the delimiter comment.
# 5  
Old 06-20-2017
Quote:
Originally Posted by rdrtx1
Update:

Code:
grep "$search" $file > /dev/null

and

Code:
sed -i 's?'"$search"'?'"$replace"'?' $file

Thanks for the help. This worked for me.

---------- Post updated at 08:00 PM ---------- Previous update was at 06:41 PM ----------

I want to replace the text if i find some keyword in the same line. Can someone suggest for that?

Code:
sed -i 's?'"${search}"'?'"${replace}"'?' $file

I want to include a keyword and if it finds then only replace should happen.
# 6  
Old 06-20-2017
Code:
sed -i '?'"$keyword"'?s?'"${search}"'?'"${replace}"'?' $file

# 7  
Old 06-21-2017
Code:
sed -i.bak "\\|${keyword}|s|${search}|${replace}|" file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace character in shell scripting

Hi, I need to replace the space " " with underscore "_" using shell scripting. The data are inside the text file. Is there are any simple code to that.? (3 Replies)
Discussion started by: gopishrine
3 Replies

2. Shell Programming and Scripting

Shell Scripting , need to search and print a line that contains a specific pattern

Take example of below file. abc.txt nas1:/abc/test/test1 /test nas1:/abc/test/test1/test2 /test/abc nas1:/abc/test/ Now i have a variable that contains "nas1:/abc/test/test1" value , so i need to search the above file for this variable and print only this line. ... (14 Replies)
Discussion started by: mohit_vardhani
14 Replies

3. UNIX for Beginners Questions & Answers

Shell Scripting , need to search and replace a string in AIX

Hi Guys, I need to search and replace a string in AIX using variables and should be case insensitive. I am able to search and replace using below command but its not working as case insensitive. cat abc.txt | sed -e 's/$a/$b/g' > abc.txt But i need to perform this with case... (9 Replies)
Discussion started by: mohit_vardhani
9 Replies

4. Shell Programming and Scripting

Search and replace in shell

I have a server.xml in about 50 instances of JBOSS servers which has the following line <Engine defaultHost="localhost" name="jboss.web"> I need to append something into this line based on the hostname . For example hostname abcdprod40j.corp.abc.net <Engine defaultHost="localhost"... (1 Reply)
Discussion started by: gubbu
1 Replies

5. UNIX for Dummies Questions & Answers

Shell script for search and replace by field

Hi, I have an input file with below data and rules file to apply search and replace by each field in the input based on exact value or pattern. Could you please help me with unix script to read input file and rules file and then create the output and reject files based on the rules file. Input... (13 Replies)
Discussion started by: chandrath
13 Replies

6. Shell Programming and Scripting

guidance required: feed load search & display in shell scripting

Hi All, I am fairly new to Shell Scripting, however learning fast ;-) I have been asked to do the below basic shell script :confused: There are few feed files we are recieving in the server from multiple locations spread out during the day, rite now we are checking manually each file... (2 Replies)
Discussion started by: sachaan
2 Replies

7. Shell Programming and Scripting

How to replace word with multiline text using shell scripting.

Hi all I have data files which contain data as shown below: Line 5: FIDE INST_DESC: DIAM Co Ltd/Japan => MAID Co Ltd/Japan INST_NME: DIAM Co Ltd/Japan => MAID Co Ltd/Japan Line 6: FIDE INST_DESC: DIAM DL/Pimco US Bond Open Born in the USA => MAID DL/Pimco US Bond Open Born in the... (6 Replies)
Discussion started by: Ganesh_more
6 Replies

8. Shell Programming and Scripting

how to find files and replace them in a directory in Shell scripting

I have a directory /java/unix/data In data directory i have so many files from which i want to find some files who look alike below.(there are number of such below such files as well different files too in the data directory) -68395#svg.xml -56789#ghi.xml -67894#gjk.org -56734#gil.txt I... (6 Replies)
Discussion started by: pratima.kumari
6 Replies

9. UNIX for Dummies Questions & Answers

Find and replace in all files using shell scripting

Hi all, I'm looking to find and replace a string in all HTML files within a certain directory, including subdirectories. Normally, I would play with this a little to get it to work, but I can't mess this up, so I'm going to ask here. Basically, I want to find "<title>" in all *.htm* files... (11 Replies)
Discussion started by: slothario
11 Replies

10. Shell Programming and Scripting

search and replace dynamic data in a shell script

Hi, I have a file that looks something like this: ... 0,6,256,87,0,0,0,1187443420 0,6,438,37,0,0,0,1187443380 0,2,0,0,0,10,0,1197140320 0,3,0,0,0,10,0,1197140875 0,2,0,0,0,23,0,1197140332 0,3,0,0,0,23,0,1197140437 0,2,0,0,0,17,0,1197140447 0,3,0,0,0,17,0,1197140543... (8 Replies)
Discussion started by: csejl
8 Replies
Login or Register to Ask a Question