How to substitute a word in multiple file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to substitute a word in multiple file?
# 1  
Old 10-24-2017
How to substitute a word in multiple file?

Team,

I want to change below parameter in all the files in a directory,

Check for HOSTNAME=`hostname`
Change to HOSTNAME=localhost

And I tried below but, its not working ☹

Code:
find /tmp -type f -exec sed 's/"HOSTNAME\=\`hostname\`"/"HOSTNAME\=localhost/g'"

Help me if I am missing something ...

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 10-24-2017 at 12:11 PM.. Reason: Added CODE tags.
# 2  
Old 10-24-2017
Wildly guessing what might be "not working" I came up with
Code:
find /tmp -type f -exec sed 's/HOSTNAME=`hostname`/HOSTNAME=localhost/g' {} \;

Does this do what you want? But - this will output all modified files to stdout; correct redirections may not be that easy to find.

Last edited by RudiC; 10-24-2017 at 12:44 PM.. Reason: Removed escaping the "=" char as proposed by rdrtx1.
# 3  
Old 10-24-2017
Use -i option for sed (if supported). No need to escape the "=" (or maybe for some versions of sed).
# 4  
Old 10-24-2017
Quote:
Originally Posted by RudiC
Wildly guessing what might be "not working" I came up with
Code:
find /tmp -type f -exec sed 's/HOSTNAME=`hostname`/HOSTNAME=localhost/g' {} \;

Does this do what you want? But - this will output all modified files to stdout; correct redirections may not be that easy to find.
My bad, still its not get substituting, I used below one
Code:
find /tmp -type f -exec sed 's/HOSTNAME=`hostname`/HOSTNAME=localhost/g' {} \;

Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags.
It makes it easier to read and preserves spaces for indenting or fixed-width data.

Last edited by rbatte1; 10-24-2017 at 01:44 PM.. Reason: Added CODE tags
# 5  
Old 10-24-2017
Sorry: no info - no help.
# 6  
Old 10-24-2017
Quote:
Originally Posted by RudiC
Sorry: no info - no help.
I mean after executing the command the pattern is not getting changed/substituted with the new one. .. It remains the same old one...

---------- Post updated at 12:31 PM ---------- Previous update was at 12:24 PM ----------

Quote:
Originally Posted by natraj005
I mean after executing the command the pattern is not getting changed/substituted with the new one. .. It remains the same old one...
It is working if the dirctory have only one file, but in my condition I have around 40 files in the direcyiry and in that 40 files around 7 files will have my search pattern and that has to be changed with new pattern.
# 7  
Old 10-24-2017
I mean it worked perfectly on my linux system, and so there's no reason to believe it doesn't on yours unless you show us
- your OS, shell, find, and sed versions
- input data (file names, directory contents, files' contents)
- DETAILED error description
- system and / or error messages

What do you expect people in here to work upon?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To substitute multiple variable by their content in a file

Dear All, I would like to instantiate a "pattern file" substituting "variable name" by their content. Saying, we have: 1/ a "pattern file" containing different "variable name", the first character of the "variable name" is "@": $ cat TPTModl.txt DEFINE... (12 Replies)
Discussion started by: dae
12 Replies

2. Shell Programming and Scripting

In Vi "sed" substitute word on a specific line

i need to substitute word on a specific line. I was able to do it on command line like below but it is not working in vi. command line like below: sed -e '8s/table_name/schema.table_name/' file_name. in vi table_name and schema are my positional parameters that i pass into the script. ... (5 Replies)
Discussion started by: pimmit22043
5 Replies

3. Shell Programming and Scripting

Substitute one line of multiple files according to another file

I need to make ~96 configure files from a template config file which has hundreds of rows that looks like: template.config: #average insert size avg_ins=1000 ...... other information omitted Those config files are named in sequence from S01.config, S02.config, ... etc with different... (11 Replies)
Discussion started by: yifangt
11 Replies

4. Shell Programming and Scripting

removing a word in a multiple file starting at the dot extension

hi I would like to ask if someone knows a command or a script on how to rename a multiple file in the directory starting at the end of the filename or at the .extension( i would like to remove the last 11 character before the extension) for example Below is the result of my command ls inside... (5 Replies)
Discussion started by: jao_madn
5 Replies

5. Shell Programming and Scripting

Perl: substitute multiple lines

I'd like to do this using Perl command line (sorry no awk, sed, etc.) replace the 3 middle lines in a file by one line: aaa 123 bbb 234 ccc 34567 dd 4567 ee 567 to: aaa 123 AAA ee 567 I tried this but not working: perl -pi -e "s/aaa\ 123\nbbb\ 234\nccc\ 34567/AAA/" file (2 Replies)
Discussion started by: tintin78899
2 Replies

6. Shell Programming and Scripting

howto substitute word in vi command mode

Hi I'm trying to substitute word "January" with word "March" in my script but in certain lines only. I have couple words containing pattern that I want to substitute e.g. - log.January.1.array - log_January_1_array_1 - log.January.1.array - log_January_1_array_11 Trying to do cmd below... (9 Replies)
Discussion started by: presul
9 Replies

7. Shell Programming and Scripting

AWK Multiple substitute

I want to reformat the following: ID1 ID001 0 0 2 1 GG TC GG CT GG AA AA AG ID2 ID002 0 0 2 2 GG 00 AG CC GG GG TC CC I want to replace only:... (1 Reply)
Discussion started by: genehunter
1 Replies

8. Shell Programming and Scripting

Can "sed" substitute word on a specific line?

Hello experts, I know line number of the word I want to replace. Can "sed" substitute word on a specific line? As well, can sed substitute words inside a specific patten. ex. <word>lalala</word> #replace anything between <word> and </word> minifish (2 Replies)
Discussion started by: minifish
2 Replies

9. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies

10. Shell Programming and Scripting

how to substitute more than one word in a text file?

well i have this file here: <XML> <pregate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <system_c>HPREGATE</system_c> <trans_c>HSPG</trans_c> <trans_dt>20060105161333</trans_dt> <user_id_m></user_id_m> <func_c>C</func_c> </pregate> </XML> i want to... (2 Replies)
Discussion started by: forevercalz
2 Replies
Login or Register to Ask a Question