Find ,replace and overwrite from master and sub dir


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find ,replace and overwrite from master and sub dir
# 1  
Old 11-15-2010
Data Find ,replace and overwrite from master and sub dir

Hello experts,

Can someone please help me handling the below situation.

I have files in multiple directories as below.

/data/input/A.txt
/data/input/a1.txt
/data/input/adhoc/b.txt
/data/input/adhoc/b1.txt
/data/input/adhoc/temp/c.txt
/data/input/adhoc/temp/d.txt

where some of the files contain a particular pattern, for example 'replace_me'. (assuming a1.txt b1.txt and d.txt contains that pattern )

I need to identify those files containing 'replace_me' and replace the content with 'You_are_replaced', so that the original files are replaced.

hence
/data/input/a1.txt
/data/input/adhoc/b1.txt
/data/input/adhoc/temp/d.txt files should contain the replaced text.

Can someone please help me out replacing the original file with the new one.

Your help is much appreciated.

Thanks a lot.
pasupuleti
# 2  
Old 11-15-2010
This will affect(recursively) all txt files under /data/input/ that match the pattern
Quote:
find /data/input/ -name *.txt | xargs sed -i 's/replace_me/You_are_replaced/g'
# 3  
Old 11-15-2010
Code:
find /data/input -type f -name "*.txt" -exec grep -l replace_me {} \; | xargs sed -i 's/replace_me/You_are_replaced/g'

# 4  
Old 11-16-2010
Thank you kevintse and ctsgnb !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to find out which server is LDAP/NIS master?

I have some scripts where I go and do some LDAP/NIS queries to report on users on Unix servers. What I am trying to figure out is, how do I know which server is the LDAP? The configs have been in place long before I took over so was hoping someone had some insight on how to figure this out. I must... (1 Reply)
Discussion started by: dagamier
1 Replies

2. UNIX Desktop Questions & Answers

How can I replicate master master and master master MySQL databse replication and HA?

I have an application desigend in PHP and MySQl running on apache web server that I is running on a Amazon EC2 server Centos. I want to implement the master-master and master slave replication and high availability disaster recovery on this application database. For this I have created two... (0 Replies)
Discussion started by: Palak Sharma
0 Replies

3. Shell Programming and Scripting

awk search/replace specific field, using variables for regexp & subsitution then overwrite file

Hello, I'm trying the solve the following problem. I have a file which I intend to use as a csv called master.csv The columns are separated by commas. I want to change the text on a specific row in either column 3,4,5 or 6 from xxx to yyy depending upon if column 1 matches a specified pattern.... (3 Replies)
Discussion started by: cyphex
3 Replies

4. Shell Programming and Scripting

KSH - Find paths of multiple files in CC (dir and sub-dir))

Dear Members, I have a list of xml files like abc.xml.table prq.xml.table ... .. . in a txt file. Now I have to search the file(s) in all directories and sub-directories and print the full path of file in a output txt file. Please help me with the script or command to do so. ... (11 Replies)
Discussion started by: Yoodit
11 Replies

5. Shell Programming and Scripting

how to find a file then overwrite with a newer version

This should be a simple script, but can't find one with google search. I just need to find the file that is in many directories, then overwrite that file with a newer version i.e. find file.jar then overwrite with /root/file.jar All I get in searches is substitute text with new test inside... (1 Reply)
Discussion started by: haircat
1 Replies

6. Shell Programming and Scripting

what is the find command to find exact dir from the root

I want to find a dir called STOP from the root.so what is the find command. Thanks & Regards Rajkumar (1 Reply)
Discussion started by: rajkumar_g
1 Replies

7. Shell Programming and Scripting

awk search and replace and overwrite file

hi, i am doing awk '{gsub("hello", "bye", $0); print}' test.dat but I want to actually store the results of this global change into the file test.dat itself, i.e. I don't want the results to come to stdout, I want the results to overwrite the initial file how can I do this? thanks (3 Replies)
Discussion started by: JamesByars
3 Replies

8. Shell Programming and Scripting

A script to find dir, delete files in, and then del dir?

Hello!! I have directories from 2008, with files in them. I want to create a script that will find the directoried from 2008 (example directory: drwxr-xr-x 2 isplan users 1024 Nov 21 2008 FILES_112108), delete the files within those directories and then delete the directories... (3 Replies)
Discussion started by: bigben1220
3 Replies

9. Programming

To find a existence of a dir within a dir

Hi, I want to find whether a dir "temp" is present inside a dir. It should get a dir a input and search recursively within that directory to check whether temp is present and return 1 or return 0 if it is not present anywhere inside the directory/sub-directory. I know we can use readdir in the... (1 Reply)
Discussion started by: spsenthil
1 Replies

10. Shell Programming and Scripting

a script to clone a dir tree, & overwrite the dir struct elsewhere?

hi all, i'm looking for a bash or tcsh script that will clone an empty dir tree 'over' another tree ... specifically, i'd like to: (1) specify a src directory (2) list the directory tree/hiearchy beneath that src dir, w/o files -- just the dirs (3) clone that same, empty dir hierarchy to... (2 Replies)
Discussion started by: OpenMacNews
2 Replies
Login or Register to Ask a Question