How to Replace a Multi Line String in a Bunch Files

raplace multi line long text in multiple files (notepad++ or other solution)

Visual Studio Code (https://code.visualstudio.com/) can do that.

Open the folder as a project and go to Edit -> Replace in Files.

When typing your text, you need to type Shift-Enter to type multiple lines. A simple Enter will start the search, Ctrl-Alt-Enter will replace all occurrences.

VSCode Replace in Files

Python - replace multiline string in a file

The simple solution:

  1. Read the whole text into a variable as a string.
  2. Use a multi-line regexp to match what you want to replace
  3. Use output = pattern.sub('replacement', fileContent)

The complex solution:

  1. Read the file line by line
  2. Print any line which doesn't match the start of the pattern
  3. If you find a match for the start, stop printing until you see the end pattern.
  4. If you saw the end pattern, print the replacement

How to replace a string in multiple files in linux command line

cd /path/to/your/folder
sed -i 's/foo/bar/g' *

Occurrences of "foo" will be replaced with "bar".

On BSD systems like macOS, you need to provide a backup extension like -i '.bak' or else "risk corruption or partial content" per the manpage.

cd /path/to/your/folder
sed -i '.bak' 's/foo/bar/g' *

How to do multi string replace in a multiline text file that has almost a million lines using PowerShell?

Your only problem is the use of inline regex option s, i.e. the SingleLine option that makes metacharacter . match any character, including newlines; in effect, this causes your regex to match the entire string, across all lines; without this option, . matches any character other than a newline, which is what you're looking for here.

Also note that character class \s matches any whitespace character, and therefore also newlines; while that isn't a problem in your case, it is better to simply match a (space) verbatim, if you know that only a space can occur in that position.

Finally, note that it is sufficient to use one capture group, because you needn't capture the separator string, given that you're replacing it with a fixed character, and you need not capture the remainder of the string, given that it should remain unchanged.

Therefore (note that inline option m (Multiline) is still needed to make ^ match the start of each line):

(Get-Content -Raw ..\sample-mosquitto.log) -replace '(?m)^(\d{10}): ', '$1|'

Re performance:

Assuming the file fits into memory as a whole (which is typically true even for large text files), use of the -Raw switch to read the file into a single, multi-line string is indeed the fastest way to process the file; by contrast, using Get-Content without -Raw would result in line-by-line streaming, which is not only inherently slower, but slowed down by each line getting decorated with metadata - see GitHub issue #7537 for a discussion about a potential future opt-out.

However - again assuming that the file fits into memory as a whole - there is a way to speed up Get-Content's line-by-line processing, namely via -ReadCount 0, which causes all lines to be emitted as a single array (which requires only a single output object, which only as a whole is decorated with metadata):

# Slower than -Raw, but reasonably fast line-by-line processing,
# thanks to -ReadCount 0
(Get-Content -ReadCount 0 ..\sample-mosquitto.log) -replace '^(\d{10}): ', '$1|'

How to use sed to replace multiline string?

I realize this is not what you asked by maybe its worth not using sed?

How about a python solution? It walks directory passed as first parameter to script and replaces exactly <Directory element as you wrote it while only changing None to All and writes changes back to the file. It will also work with different indentation levels while preserving original indentation. Works on both python2 and python3.

After all i am assuming if you have sed you probably have python too.

#!/usr/bin/env python
import re

r = re.compile(r'(<Directory /var/www/>\s+Options Indexes FollowSymLinks\s+AllowOverride )None(\s+Require all granted\s+</Directory>)', re.MULTILINE)

for root, dirs, files in os.walk(sys.argv[1]):
for file_name in files:
if file_name.endswith('.conf'):
file_path = os.path.join(root, file_name)
with open(file_path) as fp:
data = r.sub(r'\1All\2', fp.read())
with open(file_path, 'w+') as fp:
fp.write(data)

Replace a word with multiple lines using sed?

If you build your multiple line text with "\n"s, this will work with a simple sed command as:

DATA=`echo ${DATA} | tr '\n' "\\n"`
#now, DATA="line1\nline2\nline3"
sed "s/_data_/${DATA}/" mail.tpl


Related Topics



Leave a reply



Submit