Shell script to find and replace contents of files in directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to find and replace contents of files in directory
# 1  
Old 04-03-2013
Ubuntu Shell script to find and replace contents of files in directory

Hi all

This is my first post. Please bear with me with all my mistakes. I started learning shell since couple of days now and this might be quite basic for all, i want to search for files in a directory containing specific string and replace it with new string. The code i wrote is quite bulky for this simple task and i started to shrink it. I was struck with this line here.

Code:
sed '/s/abc/def/g' > tmp/copy.tmp
mv tmp/copy.tmp $file

This piece of code is working absolutely fine but when i tried to redirect output directly to the $file then $file is nullified. can someone please explain me why this is happening and is there a way to replace string in just one line of code?

Code:
sed '/s/abc/def/g' > $file


Many Thanks & Regards,
Raj
# 2  
Old 04-03-2013
If you open the file for output, that overwrites it before reading the content, so although no input is then found so there is no processing, the damage has been done.

We've all wondered about this at some point, so don't apologise.


I hope that this helps,
Robin
Liverpool/Blackburn
UK
This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 04-05-2013
Thanks a lot rbattle1 for your quick reply. Your answer solved my confusion....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to find the GB files in /tmp directory in remote server

Hi, i need help on shell scripting. Main intention of the script is step 1: ssh to remote server Step 2: cd /tmp in remote server Step 3: in tmp i want to grep only files and directories which are in GB sizes All the servers list file is - tmpsrv.txt vi tmpsrv.txt ... (17 Replies)
Discussion started by: kumar85shiv
17 Replies

2. Shell Programming and Scripting

Find and Replace in Shell script

Friends, I have more than 1000 lines in text file which needs to be converted as UPPERCASE by adding _ com.sun.url=www.sun.com com.ssl.port=808 com.ui.path=/apps/ssi Expected output com.sun.url=_COM.SUN.URL_ com.ssl.port=_COM.SSL.PORT_ com.ui.path=_COM.UI.PATH_ Thanks in... (4 Replies)
Discussion started by: baluchen
4 Replies

3. 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

4. Shell Programming and Scripting

Shell Script to store contents of multiple files into one

Hi, I"m writing a script to store all the contents of multiple files with different file names into one single file. I am giving in only last modified date of file in a folder. The below script gives a list of just one file based on the input date i give which is taken as string variable. I... (2 Replies)
Discussion started by: ashrocks
2 Replies

5. 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

6. Shell Programming and Scripting

shell script to find and replace string in multiple files

I used the following script cd pathname for y in `ls *`; do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y; done and it worked fine for finding and replacing strings with names etc. in all files of the given path. I'm trying to replace a string which consists of path (location of file) ... (11 Replies)
Discussion started by: pharos467
11 Replies

7. Shell Programming and Scripting

Shell:Find a word in files in a directory and subdirectories

I'm looking to write a ksh code with will be alble to find a word like 'toto' in all files going from my current directory. eg. /doc ----------->have: text.c which "toto" /doc/usr-------->have: build.pc, help.java which "toto" /doc/usr/cach -->have: test.sh which "toto" /doc/build... (4 Replies)
Discussion started by: yeclota
4 Replies

8. Shell Programming and Scripting

find and replace string in a directory files

Hi, I have a directory has DIR1 and the D1 directory has 200+ files. I want change the string from "Bangalore" to "Bangaluru" in all files in the D1 directory. Thanks (2 Replies)
Discussion started by: koti_rama
2 Replies

9. Shell Programming and Scripting

shell script to find and copy the files creted in the year 2006 to another directory

Hi All, I am new to UNIX. I will be thankful if some one helps me. I have to write a shell script for one of the requirement. I have files created from Jan 2006 to March 2008. My requirement is to write a script in such a way that 1) To find and copy(not Moving) the files created in the... (2 Replies)
Discussion started by: manas6
2 Replies

10. Shell Programming and Scripting

Find and Replace in multiple files (Shell script)

hi guys, Suppose you have 100 files in a folder and you want to replace all occurances of a word say "ABCD" in those files with "DCBA", how would you do it ??? jatin (13 Replies)
Discussion started by: jatins_s
13 Replies
Login or Register to Ask a Question