adramelech
adramelech

Reputation: 3

Finding strings and then replacing them from a list

Alright, I have this problem of having too many lines and too many different files to edit one by one, I need to come up with a way to do this. Regexp probably work, but I just don't know why.

I have a list like this:

    <a class="jTip">First</a>
    <a class="jTip">Second</a>
    <a class="jTip">Third</a>
    <a class="jTip">Fourth</a>

And it is a long list (around 6000 lines). There is also around 50 files with lines like this:

<a class="jTip">Second</a>, <a class="jTip">Fifth</a>

So what i want to do is to find a way to get the <a class="jTip">First</a> line from the list, search for it in these files, and replace it with <a class="jTip" name="something">First</a>.

My question is, is there a way to make regexp get lines from a list, then find and replace those lines in other files (using notepad++, but i'm open to suggestions).

EDIT: I think I didn't express my problem correctly. I have a long list of strings, around 6000 lines. And I have around 30 other files that includes these lines in different combinations. I need a way to do this:

Get the first line from the list file, find it in other files, replace them with a modified version of the line. Then get the second line from the list file, find it in other files, replace them with a modified version of the line. Then get the third line from the list file, find it in other files, replace them with a modified version of the line.

and i have to do this 6000 times (each line is different!). I cannot do it like:

find <a class="jTip">First</a> replace <a class="jTip" name="something">First</a>, because that something will change (I can do that manually, doesn't change very often). So for the first source/list file it will be something and then i'll run the same files for the second list and it will be anything. I cannot mass replace (I know how to replace all) <a class="jTip"> to <a class="jTip" name="something">.

Sadly, i'm working on a mess of a file, so I don't even think there is a way to do this with regex. I thought I'd give it a shot.

Also, sorry for the wrong tags, I fixed it.

Lastly, is there a way to "get each (any char) from a file and replace it with (same chars) in other files" thing in regex?

Upvotes: 0

Views: 83

Answers (4)

Quanmao
Quanmao

Reputation: 92

You can use msr.exe / msr.gcc48 to do it with just 1 command line : extract -> search -> replace, in tools directory in my open project: https://github.com/qualiu/msr

Assume your list file is source-list.txt

Assume the recusive directories/files to replace are paths-to-find like: dir1,dir2,file1,file2

Extract from list, preview 3 lines:

msr -p source-list.txt -it ".*<a class=\"jTip\">(\w+).*" -H 3

Search and replace recursively in paths-to-find:

  • Windows: (Use %%a instead of %a if you want save it in a file like my.bat/my.cmd to run:

    for /f "tokens=*" %a in ('msr -p source-list.txt -it ".*<a class=\"jTip\">(\w+).*" -o "$1" -PAC') do msr -rp paths-to-find -it "(<a class=\"jTip\" name=\"something\">)\w+" -o "${1}%a" -R

  • Linux:

    for a in $(msr -p source-list.txt -it ".*<a class=\"jTip\">(\w+).*" -o '$1' -PAC); do msr -rp paths-to-find -it "(<a class=\"jTip\" name=\"something\">)\w+" -o '${1}'$a'' -R; done

    or

    for a in $(msr -p source-list.txt -it '.*<a class="jTip">(\w+).*' -o '$1' -PAC); do msr -rp paths-to-find -it '(<a class="jTip" name="something">)\w+' -o '${1}'$a'' -R; done

Upvotes: 0

Sindhoor
Sindhoor

Reputation: 548

Open file press ctrl+f or ctrl+h (or go to find and replace option) type class="jTip"> in find box and replace it with class="jTip" name="something">

No simpler answer will be awailable than this.

Upvotes: 0

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

You can use JQuery and achieve what you want:

$(document).ready(function(){
  $('.jTip').each(function(){
     if($(this).text() === 'First' || 
     $(this).text() === 'Second' ||
     $(this).text() === 'Third'){
       $(this).attr('name','something');
       $(this).text('First');
     }
  });
})
a[name='something']{
  color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="jTip">First</a><br/>
<a class="jTip">Second</a><br/>
<a class="jTip">Third</a><br/>
<a class="jTip">Fourth</a><br/>
<a class="jTip">Fifth</a><br/>
<a class="jTip">First</a><br/>
<a class="jTip">Second</a><br/>
<a class="jTip">Third</a><br/>
<a class="jTip">Fourth</a><br/>
<a class="jTip">Fifth</a>

To illustrate that the name something has been added to the element i have used a color css for it.

Upvotes: 0

Kristianmitk
Kristianmitk

Reputation: 4778

If you are running on a linux system, than one solution could also be using the cli programm sed.

sed -i  -e 's/<a class="jTip">First</a>/<a class="jTip" name="something">First</a>/g' {pathToFolder}/*

NOTE: don't forget to replace {pathToFolder} with your actual path

Upvotes: 1

Related Questions