Search and replace in shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and replace in shell scripting
# 8  
Old 06-21-2017
Your new requirement is not clear. If what Scrutinizer and rdrtx1 suggested don't work for you, we need more information... Do you only want to change a line if that line contains the keyword? Or do you want to change any line in the file if the keyword appears on any line in the file?

Are there any special characters (like forward slashes, question marks, single quotes, double quotes, vertical bars, or parentheses) in the keyword like there are in both the search string and the replacement string?

Last edited by Don Cragun; 06-21-2017 at 05:29 AM..
# 9  
Old 06-21-2017
Both the codes are not working for me.

I need to replace one text in case of multiple occurrences
example :

SSL "none"
none
none is available


So here i want to replace "none" with "nothing" when I find SSL in the same line of none.

For this i need help.

So for this i have used script like this.

Code:
search="none"
replace="nothing"
for file in `find -name 'test.txt'`; do
if grep -q "$search" "$file"; then
sed -i  "\\|${SSL}|s|${search}|${replace}|" $file
else
echo "Search string not found in $file!"
fi
done

---------- Post updated at 01:04 PM ---------- Previous update was at 12:57 PM ----------

Hi Don Cragun,

Yes, I want to change a line if that line contains a keyword.

Yes there are special characters in the search and replace.

Here is the original requirement.
Search
Code:
SSLMutex "none"

Replace
Code:
SSLMutex file:/var/opt/<SID>_<HOSTNAME>/Apache/ssl_mutex

But none keyword is having multiple occurrences. that's the reason i want to use the logic where i can replace a line if that line contains a keyword.

Last edited by avmk0407; 06-21-2017 at 02:00 PM.. Reason: adding code
# 10  
Old 06-21-2017
Why not do a search/replace on the entire line?
# 11  
Old 06-21-2017
Please become accustomed to provide decent context info of your problem.
It is always helpful to support a request with system info like OS and shell, related environment (variables, options), preferred tools, and adequate (representative) sample input and desired output data and the logics connecting the two, and error messages when availabe, to avoid ambiguities and keep people from guessing.





Quote:
Originally Posted by avmk0407
Both the codes are not working for me.
.
.
.
because you are expandig an uninitialized variable: ${SSL}. Either assign something to the var, or use a string constant:
Code:
sed  "\\|SSL|s|${search}|${replace}|" file
SSL "nothing"
none
none is available

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