-2

Within the content of a couple large text files, I am aiming to replace all occurrences of a specific character string with a new character string, simultaneously for 300 different character strings.

Is there any way I can do this using a comma or tab-separated search-and-replace matrix such as this? (the actual character strings vary widely in their length and type of characters, but does not contain , or TAB)

currentstring1,newstring1
currentstring2,newstring2
currentstring3,newstring3
aB9_./cdef,newstring4
.
currentstring300,newstring300
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
user3026965
  • 673
  • 4
  • 8
  • 22
  • Where are the files located? Why have you tagged unix and notepad++ both? – Utsav May 23 '17 at 15:36
  • 1
    Stack Overflow is not a code writing service, it's expected that you attempt to code this yourself. I would suggest you do some research on your issue (maybe try the search box at the top of the page) and make an attempt at writing some code yourself. If/when you come across any issues with your code ask again and explain what you have tried, and why it did not work for you. See [How to Ask](http://stackoverflow.com/questions/how-to-ask) for help with asking a great question. – henrycarteruk May 23 '17 at 15:54
  • to: utsav: files are either on the server (website) or on a local disk. i am trying to get solutions by either/any of the listed options (unix, notpad++, vi, etc.) this is how far i got: `perl -pi -e 's/FINDTEXT/REPLACETEXT/' * */* */*/* */*/*/* '` thanks! – user3026965 May 23 '17 at 16:41
  • jamesC: fyi: [https://stackoverflow.com/questions/11389466/...](https://stackoverflow.com/questions/11389466/multiple-word-search-and-replace-in-notepad). **absolutely zero** "code" or attempt presented yet a **highly rated Q&A**. my question is not a code writing; it is looking for a simple command that points to a matrix. – user3026965 May 23 '17 at 17:16
  • ..python is not an option in my case. – user3026965 May 23 '17 at 17:35
  • The rules on this site evolve slowly. You have referenced a five year old question as a precedant as to why you should be allowed write a poor question. Please read the [help] pages to find the current rules and guides about how to write a good question. Your comment about wanting a command suggests that this question should be closed as wanting off-site resources. – AdrianHHH May 24 '17 at 08:15
  • pls stick to the **facts**. point out what part of my question exactly makes it not a "good" question, _as you_ claim, per the SO guidelines? your corrections are nothing else but stylistic. – user3026965 May 26 '17 at 08:18
  • [AdrianHHH](https://stackoverflow.com/users/546871/adrianhhh): "Your comment about wanting a command suggests that this question should be closed as wanting off-site resources." would you mind to elaborate on this, as it makes not much sense as it is? am i not supposed to ask for input on a search and replace command help? – user3026965 May 26 '17 at 08:29
  • Your question does not show that you have made any effort to solve the problem yourself, so we do not know your expertise. Hence the question could be interpreted as you are looking for a tutorial or for a recommendation on a program to use. In a comment you wrote *"my question is ... is looking for a simple command"* which, again, is wanting a recommendation on off site resources. See item 4 of https://stackoverflow.com/help/on-topic . Finally @user3026965, to reply to an individual type an "@" then start typing their name. – AdrianHHH May 26 '17 at 09:10

2 Answers2

0

Here is something to get you started. If the replacement file is ~300 lines, then Import-Csv should be ok. However, if the file in which to replace strings is large, Get-Content will be a problem. It will try to read the entire file into memory. You will need to iterate over the file reading line-by-line.

[cmdletbinding()]
Param()

$thefile = './largetextfile.txt'
$replfile = './repl.txt'

$reps = Import-Csv -Path $replfile -Header orgstring,repstring
foreach ($rep in $reps) {
    Write-Verbose $rep
}

$lines = Get-Content -Path $thefile
foreach ($line in $lines) {
    Write-Verbose $line
    $newline = $line

    foreach ($rep in $reps) {
        $newline = $newline -replace $rep.orgstring,$rep.repstring
    }

    Write-Verbose $newline
}
lit
  • 14,456
  • 10
  • 65
  • 119
  • This will run on Linux/UNIX using PowerShell Core 6. https://github.com/PowerShell/PowerShell – lit May 24 '17 at 02:29
0

On the server, unix: 1. Make the rename matrix as below in a text editor, then copy it. 2. In the server dir where the files located, paste the multi-line rename matrix as is. 3. Enter. 4. Some characters (like slashes) may need to be escaped if present in the string, and the * at the end may be replaced to specify files.

perl -pi -e 's/FINDTEXT1/REPLACETEXT1/g' *
perl -pi -e 's/FINDTEXT2/REPLACETEXT2/g' *
perl -pi -e 's/FINDTEXT3/REPLACETEXT3/g' *
user3026965
  • 673
  • 4
  • 8
  • 22