[英]Extracting lines by pattern matching from multiple files and writing them to another file in linux
I have 20 files.我有20个文件。 I want to extract lines by matching pattern
'<script src="{%.*%}>'
from those 20 files and write those lines to new file.我想通过匹配模式
'<script src="{%.*%}>'
从这 20 个文件中提取行并将这些行写入新文件。
I also want to delete those lines from the original files.我还想从原始文件中删除这些行。
Is there a better way to do it other than what I have tried which is terrible?除了我尝试过的可怕的方法之外,还有更好的方法吗?
This is my attempt:这是我的尝试:
import os
import sh
folder_path='/home/username/folder/'
match_phrase = '<script src="{%'
new_file = '/home/username/file.txt'
files = os.listdir(folder_path)
print files
for file in files:
full_filename = folder_path + file
lines=[]
line_nos=[]
with open(full_filename) as myFile:
for num, line in enumerate(myFile, 1):
if match_phrase in line:
line_nos.append(num)
lines.append(line)
print lines
print line_nos
with open(new_file,'a') as newfile:
for line in lines:
newfile.write(line)
for del_line in line_nos:
print "deleting line %s from file=%s"%(del_line,full_filename)
del_line=str(del_line)+'d'
sh.sed('-i',del_line,full_filename)
Using sed :使用sed :
sed -i -e "/$pattern/w $newfile" -e "/$pattern/d" $files
Sed 's w
command writes match to some other file. sed的
w
命令将匹配写入其他文件。 d
deletes it. d
删除它。
example:例子:
$ pattern='<script src="{%'
$ files=/home/username/folder/*
$ newfile=/home/username/file.txt
$
$ sed -i -e "/$pattern/w $newfile" -e "/$pattern/d" $files
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.