Replacing string1 with string2 in many files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replacing string1 with string2 in many files
# 1  
Old 09-16-2004
Replacing string1 with string2 in many files

I have 70 files and want to replace string1 with string2. How can i do that?.

Thanks
# 2  
Old 09-16-2004
man sed

Have you attempted to solve this problem at all? Do you have a script fragment to show us?!

To help you out - (i'd) use sed in a for loop to iterate through the files. But that's all you're getting 'til you post some of your efforts!

Cheers
ZB
# 3  
Old 09-16-2004
I want to replace 20040915 with 20040916.
# 4  
Old 09-16-2004
You still haven't posted your work so far.

However, benefit of the doubt granted that this isn't some kind of homework problem, then a script such as
Code:
#!/bin/sh

for file in /path/to/files/*
do
   sed 's/20040915/20040916/g' $file > $file.new
   rm $file
   mv $file.new $file
done

exit 0

If you want to keep the "old" copies of the files too, just remove the "rm" and "mv" lines from the script.

Cheers
ZB
# 5  
Old 09-16-2004
Its working. Thanks ZB.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace string2 by string3 where string1 is found in line

I found this in the forum that searches a file for string1, substitute all occurrences of string2 with string3. (Title: Replace string2 by string3 where string1 is found in line) >> sed -i '/string1/s/string2/string3/g' TextFile How will I perform the same sed command and only substitute... (3 Replies)
Discussion started by: apalex
3 Replies

2. Shell Programming and Scripting

Find string1, when true find string2 in reverse direction

Hello, This is a bit complicated for me. My scenario in MyFile: Search string1, When string1 is found, grep the line containing string1, go back over that line in upward direction and grep the first line containing string2. Here is an example: MyFile His email address... (17 Replies)
Discussion started by: baris35
17 Replies

3. Shell Programming and Scripting

Replace string2 by string3 where string1 is found in line

Hello, My aim is to search string1 in all lines. When found, find and replace string2 by string3 if possible. TextFile: Here is my first line Second line with string1 & string2 Not last line but it contains string1 Expected output: Here is my first line The second line with string1 &... (6 Replies)
Discussion started by: baris35
6 Replies

4. UNIX for Beginners Questions & Answers

Replace string2 with string3 on all lines starting with string1

My OS is Windows 10 and I am using Cygwin. The file 1 content is: USE solution 2; -4.000 USE solution 3; -4.000 … USE solution 29; -4.000 USE solution 30; -4.000 USE solution 31; -4.000 …. USE solution 89; -4.000 ... USE solution 202; -4.000 etc... I need to replace... (8 Replies)
Discussion started by: supernono06
8 Replies

5. Shell Programming and Scripting

Replacing extension of files

hi, i am having a certain files in a folder where i need to replace the extension with similar file name for eg 1.csv 2.csv 3.csv 4.csv i need to replace the extension 1.csv 1.txt 2.csv 2.txt 3.csv 3.txt (3 Replies)
Discussion started by: rohit_shinez
3 Replies

6. Shell Programming and Scripting

Replacing strings in various files

i'm trying to figure out the easiest way to replace a string: pineapple pineapple-reg basketball basketball-reg football foot-reg-ball i'm storing the above in a file called wordstoreplace.txt for each line above, the word in the first column is to be replaced by the word in the second... (4 Replies)
Discussion started by: SkySmart
4 Replies

7. UNIX for Dummies Questions & Answers

replacing in many files

i have 12 files.in all the files the first line will be like: H|20111213 09:23:00|267628|617 I need to replace that with current date: H|20111217 09:23:00|267628|617 Like this i need to do it for all the files. One way i can think is inside a loop, use the below code a=`sed... (1 Reply)
Discussion started by: pandeesh
1 Replies

8. Shell Programming and Scripting

awk - print record with both string1 and string2

How do I use awk to find the records in a file that contains two specific strings? I have tried piping and using awk two times, but I don't know how to do it in one action. (2 Replies)
Discussion started by: locoroco
2 Replies

9. Shell Programming and Scripting

Remove lines before string1 and after string2

Hello All... I have a text file (.ics) which I need to read into a variable but ONLY the part including and after 'BEGIN:VEVENT' and ending with END:VEVENT Anything before BEGIN:VEVENT or after END:VEVENT should be ignored. Thanks for input Jeff BEGIN:VCALENDAR VERSION:2.0... (3 Replies)
Discussion started by: uptimejeff
3 Replies

10. Shell Programming and Scripting

ps -ef | grep "string1" "string2" " "string3"

Hi all, can any one suggest me the script to grep multiple strings from ps -ef pls correct the below script . its not working/ i want to print OK if all the below process are running in my solaris system. else i want to print NOT OK. bash-3.00$ ps -ef | grep blu lscpusr 48 42 ... (11 Replies)
Discussion started by: steve2216
11 Replies
Login or Register to Ask a Question