Visit The New, Modern Unix Linux Community


HPUX find string in directory and filetype and replace string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting HPUX find string in directory and filetype and replace string
# 1  
HPUX find string in directory and filetype and replace string

Hi,

Here's my dilemma.

I need to replace the string Sept_2012 to Oct_2012 in all *config.py files within the current directory and below directories

Is this possible?

Also I am trying to find all instances of the string Sept_2012 within files in the current directory and below

I have scoured the forum and have found the following commands but cannot get any to do what ISmilieSmilie am trying to do Smilie
Code:
	  grep -rl 'Sept_2012' ./ | xargs sed -i 's/Sept_2012/Oct_2012/g'
	  
	  sed '/Sept_2012/ {n; s/Sept_2012/Oct_2012/;}'

	  awk '/Sept_2012/{x++}x{sub("Sept_2012","Oct_2012")&&x++&&x=(x==2)?0:x}1' infile
	  
	  cat sample.txt | sed -e "s/$SEARCH/$REPLACE/" >> result.txt
	  
	  perl -p -i -e 's/original text string/replacementstring/g' file


Last edited by Franklin52; 12-14-2012 at 09:22 AM.. Reason: Please use code tags for data and code samples
# 2  
Try This

Code:
#!/bin/sh
for fl in *.py ; do
mv $fl $fl.old
sed 's/Sept_2012/Oct_2012/g' $fl.old > $fl
rm -f $fl.old
done

# 3  
Actually I figured out the find command just need to know how to replace now?
Code:
find . -name "*.py" -exec grep "Oct_2012" {} \;

---------- Post updated at 01:24 PM ---------- Previous update was at 01:14 PM ----------

Thanks for the reply however the code below didn't work
Code:
#!/bin/sh
for fl in *.py ; do
mv $fl $fl.old
sed 's/Sept_2012/Oct_2012/g' $fl.old > $fl
rm -f $fl.old
done


Last edited by pure_jax; 12-14-2012 at 09:36 AM.. Reason: Please use code tags for data and code samples
# 4  
Code:
find . -type f -name ".*py" -exec sed -i 's/sep_2012/Oct_2012/g' {} \;

# 5  
Unfortunately this didn't work either
Code:
find . -type f -name ".*py" -exec sed -i 's/sep_2012/Oct_2012/g' {} \;

# 6  
Oh Sorry it should be

Code:
"*.py"

# 7  
Code:
find . -type f -name "*.py" -exec sed -i 's/Sept_2012/Oct_2012/g' {} \;

Produced the following error: -

Usage: sed [-n] [-e script] [-f source_file] [file...]
sed: illegal option -- i

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #170
Difficulty: Easy
RedHat became a subsidiary of IBM in 2019.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find and Replace string in UNIX

Hi All, Greetings. I have a .dat file which somewhere in its content contains symbol ""^ I want to replace it with "^ I tried with SED command but could not achieve what i wanted sed -e "s/'""^'/'"^'/ig" filename.dat (5 Replies)
Discussion started by: MaddyS
5 Replies

2. Solaris

How to find and replace a string?

Dear All I need to find and replace a string in a set of files. I try as : #find / -name "*"|xargs grep "Tektra"|grep -v "Tektra GSM BTS" But it doesn't work. It just finds the string in the files. I need to find and replace it.Can you please let me know how to correct it? Thank you (2 Replies)
Discussion started by: hadimotamedi
2 Replies

3. Shell Programming and Scripting

find string and replace with string in other file

Dear all, I need your help, I have file like this: file1:23456 01910964830098775635 34567 01942809546554654323 67589 26546854368698023653 09778 58716868568576876878 08675 86178546154065406546 08573 54165843543054354305 . .file2: 23456 25 34567 26 67589 27 (2 Replies)
Discussion started by: attila
2 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. Shell Programming and Scripting

Replace last 2 folder directory string with sed

Hi guys, Im trying to replace the 2 last folders name in a list of directories with a new string, but Im dont know which regex to apply. Directories list: C/my user/documents/games & music C/my user/documents/photos 09-24-2008 C/my user/settings/config ?1_2 * The last folder may have... (11 Replies)
Discussion started by: cgkmal
11 Replies

6. Shell Programming and Scripting

Replace a string in all files under a directory and its subdirectories

Hello Friends, I've been trying to write a script which finds a string and change it with another string. For this i want to search all files (with its arguments) under a spesific directory and its subdirectories. For example lets assume i want to replace an IP= 192.168.0.4 with another... (4 Replies)
Discussion started by: EAGL
4 Replies

7. Shell Programming and Scripting

Find the position of a string and replace with another string

Hi, I have a file named "Test_2008_01_21" The file contains a string "manual" that occurs many times in the file How can i find the positions of the string "manual" in the file Ex: if the string " manual " occurs three times in the file. i want to replace the second occurance of string... (6 Replies)
Discussion started by: bab123
6 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. UNIX for Dummies Questions & Answers

Find and replace character in a string

Hi all, My problem is the following: I've a script that must list all files in a directory and write this information in a text file. I've tried to get the list through ls command and then write it using msgecho msgecho "`ls $PATH_APS_JOB_ORA`" This works good but the created string... (7 Replies)
Discussion started by: callimaco0082
7 Replies

10. Programming

how to find and replace string

hi I wanted to find this char " ^M " in my file and replace it with blank space. I am using Unix system. If i give command " :%s/^M//gc " it wont work so can anyone tell what is command to find and replace thankx (3 Replies)
Discussion started by: mridula
3 Replies

Featured Tech Videos