how to replace words in mutiple files under the same directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to replace words in mutiple files under the same directory
# 1  
Old 01-23-2012
how to replace words in mutiple files under the same directory

I would like to get help to find how to replace word in files from command line instead of to vi to each file.

This is the command i am running now.
grep <old word> *
vi (file with the word found in it)
1,$s/<old word>/<new word>/g

It would very helpful if I can combine these in one commad line.
# 2  
Old 01-23-2012
Code:
for FILE in file1 file2 file3
do
        sed 's/oldword/newword/g' < "$FILE" > /tmp/$$
        cat /tmp/$$ > "$FILE".new
done

rm -f /tmp/$$

Remove the .new only once you're sure it does what you want. Overwriting your originals with bad data can be a hard mistake to recover from...
# 3  
Old 01-23-2012
If your sed supports --in-place then you could also try this:

Code:
OLD=$1
NEW=$2
for file in ./*
do
   if [ -f "$file" ] && grep -q "$OLD" "$file"
   then
       echo "Updating $file (backup to ${file}.old)"
       sed --in-place=.old "s/$OLD/$NEW/g" "$file"
   fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. Shell Programming and Scripting

Finding non-existing words in a list of files in a directory and its sub-directories

Hi All, I have a list of words (these are actually a list of database table names separated by comma). Now, I want to find only the non-existing list of words in the *.java files of current directory and/or its sub-directories. Sample list of words:... (8 Replies)
Discussion started by: Bhanu Dhulipudi
8 Replies

3. UNIX for Dummies Questions & Answers

Replace the words in the file to the words that user type?

Hello, I would like to change my setting in a file to the setting that user input. For example, by default it is ONBOOT=ON When user key in "YES", it would be ONBOOT=YES -------------- This code only adds in the entire user input, but didn't replace it. How do i go about... (5 Replies)
Discussion started by: malfolozy
5 Replies

4. Shell Programming and Scripting

Replace directory paths in multiple files at once

I need to update about 2400 files in a directory subtree, with a new directory path inside the files I need to change this occurence in all files: /d2/R12AB/VIS/apps/tech_st/10.1.2 with this: /u01/PROD/apps/apps_st/10.1.3 I know how to change single words using "find . -type f -print0 |... (6 Replies)
Discussion started by: wicus
6 Replies

5. UNIX for Dummies Questions & Answers

mutiple file string replace

I have a number of files in a directory. In each file i want to search for a string "10.10.101.1" and replace it with another string "10.10.2.1", what would be the easiest way to achieve this? Thanks in advance. (1 Reply)
Discussion started by: frankkahle
1 Replies

6. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

7. Shell Programming and Scripting

how to do search and replace on text files in directory

I was google searching and found Perl as a command line utility tool This almost solves my problem: find . | xargs perl -p -i.old -e 's/oldstring/newstring/g' I think this would create a new file for every file in my directory tree. Most of my files will not contain oldstring and I... (1 Reply)
Discussion started by: siegfried
1 Replies

8. Shell Programming and Scripting

Mutiple For loops - moving files to another directory

I need to clean out some application subdirectories from backup scripts we used to rename to various backup extensions just in case the script failed in production and we need to rollback. I will be moving these old scripts to a staging directory and then removing them after 30 days (I have the... (9 Replies)
Discussion started by: tekster757
9 Replies

9. Shell Programming and Scripting

removing mutiple files

I have a script which removes files (if they exist) Here is a cut down example of the script. Variables file1,file2 etc have already been initialized #!/bin/bash if then \rm file1 fi if then \rm file2 fi if then \rm file3 fi if then \rm file4 (9 Replies)
Discussion started by: run_time_error
9 Replies

10. Shell Programming and Scripting

Report with mutiple files.

Hi all, In the process of creating CPU reports. I've already used `sar` to create a daily file, then monthly reports for CPU usage (which is averaged across all 4 CPU's). I've now used `cpusar -P ?`(?=CPU#) to collect individual CPU data and have four files for each day which is great. The... (4 Replies)
Discussion started by: Cameron
4 Replies
Login or Register to Ask a Question