我們試圖使用sed(或其他任何真的)來查找或編寫腳本,該腳本搜索目錄中的每個文件(包括子文件夾) ,匹配開始和結束字符串,然後從文件中刪除開始,中間內容和結束字符串。使用sed在多個文件中進行搜索和替換,使用開始和結束字符串
找到我們需要的確切命令很難找到。幫助讚賞。
謝謝。
我們試圖使用sed(或其他任何真的)來查找或編寫腳本,該腳本搜索目錄中的每個文件(包括子文件夾) ,匹配開始和結束字符串,然後從文件中刪除開始,中間內容和結束字符串。使用sed在多個文件中進行搜索和替換,使用開始和結束字符串
找到我們需要的確切命令很難找到。幫助讚賞。
謝謝。
你可以用perl
做的事情是利用range operator
。
舉個例子:
#!/usr/bin/env perl
use strict;
use warnings;
while (<DATA>) {
print unless m/START_TAG_HERE/ ... m/END_TAG_HERE/;
}
__DATA__
Some text
more text
even more text
A line with START_TAG_HERE
some text to delete
don't want this
END_TAG_HERE
and this should print now.
您可以使用File::Find
以遞歸搜索的目錄結構,並做到這一點。 perl
也支持在位編輯(如sed
),但與File::Find
結合會稍微複雜。
但是,你可能會發現這樣的:
perl -i.bak -ne 'print unless m/START_TAG_HERE/ ... m/END_TAG_HERE/;' somefile
會做的伎倆。然後你可以將它與find
命令結合起來。
你可以試試下面的步驟
開始模式= '本'
結束模式= '測試'
匹配的開始和結束模式
grep -e '^This.*testing$' -R Foldername/
取下啓動模式
grep -e '^This.*testing$' -R testing/ | sed 's/\('This'\)\(.*\)/\2/'
卸下端圖案
grep -e '^This.*testing$' -R testing/ | sed 's/\('.*'\)\('testing'\)/\1/'
實施例:
匹配的開始和結束的模式:
$ grep -e '^This.*testing$' -R testing/
testing/test/file3:This is for my best testing
testing/test/file4:This is small testing
testing/file2:This is for final testing
testing/file1:This is for model testing
卸下開始圖案
$ grep -e '^This.*testing$' -R testing/ | sed 's/\('This'\)\(.*\)/\2/'
testing/test/file3: is for my best testing
testing/test/file4: is small testing
testing/file2: is for final testing
testing/file1: is for model testing
卸下端PATTEN
grep -e '^This.*testing$' -R testing/ | sed 's/\('.*'\)\('testing'\)/\1/'
testing/test/file3:This is for my best
testing/test/file4:This is small
testing/file2:This is for final
testing/file1:This is for model
刪除的開始和結束模式
grep -e '^This.*testing$' -R testing/ | sed 's/\('This'\)\('.*'\)\('testing'\)/\2/'
testing/test/file3: is for my best
testing/test/file4: is small
testing/file2: is for final
testing/file1: is for model
除去開始,結束和其中的所有內容?這就是我需要的。 – pixelkicks
你查看grep手冊頁? –
grep找到文本。爲了修改文本,你需要'sed',或者像awk或Perl這樣的語言。 –
對,我想我們可以。我們現在會做一些Google搜索(我們不是最好的Linux)。你有任何示例命令? – pixelkicks