Bulk Find and Replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bulk Find and Replace
# 1  
Old 07-26-2012
Bulk Find and Replace

Hi Friends,

I have a directory with a ton of .html files, like this

ls -m1 dir

Code:
1.html
2.html
3.html
4.html

Somewhere in the files, there is a pattern like this

1.html
Code:
http://unix.com/cgi-bin/task?taskid=12010&task.out

2.html
Code:
http://unix.com/cgi-bin/task?taskid=11110&task.out

3.html
Code:
http://unix.com/cgi-bin/task?taskid=15822&task.out

4.html
Code:
http://unix.com/cgi-bin/task?taskid=00010&task.out

These patterns can repeat any number of times in the html files.

But, the taskid is constant for each file. If it is 12010 in 1.html, it will be the same across the whole 1.html file. The value will not change. The task id value in 2.html is different.

My problem now is to replace the taks ids in all these files with 33528. So, my pattern in the html files should be replaced like this

1.html
Code:
http://unix.com/cgi-bin/task?taskid=33528&task.out

2.html
Code:
http://unix.com/cgi-bin/task?taskid=33528&task.out

3.html
Code:
http://unix.com/cgi-bin/task?taskid=33528&task.out

4.html
Code:
http://unix.com/cgi-bin/task?taskid=33528&task.out

It is like a bulk find and replace of
Code:
find taskid=* in /dir/*.html and replace taskid=33528

.

Thanks

---------- Post updated at 12:10 PM ---------- Previous update was at 11:45 AM ----------

So far, I tried this

Code:
for f in `ls *.html`; do sed 's/taskid=*/taskid=33528/' $f > $f.tmp;done

It print the following pattern in 1.html and the same in all other files (like appending 33528 to the already existing task id number).

Code:
http://unix.com/cgi-bin/task?taskid=3352812010&task.out

# 2  
Old 07-26-2012
Lots of whats to do this, but simply done like this:

Code:
$ cat *.html | sed 's/\(.*taskid=\)\([0-9]*\)\(.*$\)/\133528\3/g' 
http://unix.com/cgi-bin/task?taskid=33528&task.out
http://unix.com/cgi-bin/task?taskid=33528&task.out
http://unix.com/cgi-bin/task?taskid=33528&task.out
http://unix.com/cgi-bin/task?taskid=33528&task.out

So in a find statement:
Code:
for file in $(find /dir -name '*.html' -print); do
    sed 's/\(.*taskid=\)\([0-9]*\)\(.*$\)/\133528\3/g' ${file} > /tmp/tmp.file
    mv /tmp/tmp.file ${file}
    # If needed to fix permissions and ownership
    chmod XXX ${file}
    chown X:X ${file}
done

Edit:

You were close on your sed statement, here's what you probably were going for that route:
Code:
$ cat *.html | sed 's/taskid=[0-9]*/taskid=123456/g'
http://unix.com/cgi-bin/task?taskid=123456&task.out
http://unix.com/cgi-bin/task?taskid=123456&task.out
http://unix.com/cgi-bin/task?taskid=123456&task.out
http://unix.com/cgi-bin/task?taskid=123456&task.out


Last edited by Vryali; 07-27-2012 at 02:51 PM..
This User Gave Thanks to Vryali For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and replace

Hi Team, one silly issue. but its not working for me. I need to find a pattern a file and replace it with the given value in another file. Here's the code snippet. Search_String=100 Replace_String=151 cat ${work}/temp_${CSV_File} | sed 's|"${Search_String}"|"${Replace_String}"|g'... (2 Replies)
Discussion started by: kmanivan82
2 Replies

2. Shell Programming and Scripting

Bulk find in UNIX at unknown dir location

Dear All, I have a file which is having a number my_file.csv 032001031 100509001 203001165 there are many more about 5000. I have a path in unix /my_path/my_loc/DEV/RPD10/CPD25/WFM/RK_WFM/OUT/*/ where i will have a file like CPD25_203001165.pdf CPD25_100509001.pdf... (4 Replies)
Discussion started by: yadavricky
4 Replies

3. Shell Programming and Scripting

Shell or perl script to replace XML text in bulk

Hi, I am looking for assistance over shell or perl (without XML twig module) which replace string in XML file under particular branch..example of code file sample.. Exact requirment : Replace "Su saldo es" in below file with "Your balance" but only in XML branch of Text id=98 and Text Id=12... (7 Replies)
Discussion started by: Ashu_099
7 Replies

4. Shell Programming and Scripting

find and replace

Hi I am very new to shell scripting (and linux in general)... I am trying to build some script, that will read a person from a csv file, (each person will have an ID, eg id001abc, which will go up one by one a few hundred times) then when find and replace the matching data in an xml file. ... (4 Replies)
Discussion started by: sam.breslin
4 Replies

5. UNIX for Dummies Questions & Answers

Find and Replace

After running a command like grep -ir files2/ * This will find all the files that contain "files2/" in it. For example if it finds files2/dir/today files2/dir/yesterday files2/dir/2daysago Now it may find 100 instances, so is there a quick find and replace command I can use? I... (4 Replies)
Discussion started by: NycUnxer
4 Replies

6. Shell Programming and Scripting

find replace

Hi I am replacing some string occurances with empty string in all files under one directory using find ./ -name "*.dmp" | xargs perl -pi -e 's/\\N//g' | its taking too much time for replacing and redirecting to same file in the same directory. Similarly afterwards i am finding last... (2 Replies)
Discussion started by: dbsurf
2 Replies

7. Shell Programming and Scripting

find and replace

Hi, There are some "n" files in a directory which contains comman string.A command to find and replace the string in all the files without looping. like if i am in a directory : # find ./ -name ".txt" | xargs sed -e 's/test/tst' Upto here is performed correctly and i want to... (4 Replies)
Discussion started by: rakshit
4 Replies

8. UNIX for Advanced & Expert Users

Please help with find and replace:

Hi I am trying to find a product code hightlighted in red, and re-insert it at another place on the same file. I shall be grateful if anyone can help me with this. Stuck and have deadline!!:confused: Original Line: (I can get source data in one of these two formats) ISD=977155185403901+DIE... (2 Replies)
Discussion started by: gloovy_tb
2 Replies

9. UNIX for Dummies Questions & Answers

find and replace

I have statement like this column_id.columnname=="value" in unix i want to modify above statement to variable1=="value" that means i have to replace the string before "==" by string "variable1" second catch is, in statement instead of "==" you can have any arithmatic comarision... (7 Replies)
Discussion started by: mahabunta
7 Replies

10. UNIX for Dummies Questions & Answers

find and replace

I have a file that I want to remove all of the periods from. I am using the below to remove quotations and it works fine -I thought I could make it work for the period but it does not. Any help would be appreciated. works to remove the double quote mark (") sed -e 's/"//g' input file... (4 Replies)
Discussion started by: valhutch
4 Replies
Login or Register to Ask a Question