在多个文件中添加电子邮件地址周围的文本

我在文件夹中有许多文本文件。 其中的文字采用以下格式:

%%%%%%%%%%@yahoo.com %%%%%%@wanadoo.fr %%%%raviplywoodglasscentre@yahoo.comravi %%nameemail%%@yahoo.com %.getincontact@numberland.com %0dronbracale@roadrunner.com %1%3@example.com %1@elsewhere.com %1@example.com 

我想在所有文件文本中进行更改,并使它们如下所示:

 {"email":"%%%%%%%%%%@yahoo.com"} {"email":"%%%%%%@wanadoo.fr"} {"email":"%%%%raviplywoodglasscentre@yahoo.comravi"} {"email":"%%nameemail%%@yahoo.com"} 

我想制作一个文件夹中的完整文件。
我试过这个:

 awk '{ printf("{"email":"%s"}", $l);}' test 

但它不起作用。

那么,有没有什么办法可以修改像这样的文件夹中的所有文件中的文本?

 $ sed 's/.*/{"email":"&"}/' file {"email":"%%%%%%%%%%@yahoo.com"} {"email":"%%%%%%@wanadoo.fr"} {"email":"%%%%raviplywoodglasscentre@yahoo.comravi"} {"email":"%%nameemail%%@yahoo.com"} {"email":"%.getincontact@numberland.com"} {"email":"%0dronbracale@roadrunner.com"} {"email":"%1%3@example.com"} {"email":"%1@elsewhere.com"} {"email":"%1@example.com"} 

所以要对你可以做的所有文件采取行动

 sed -i 's/.*/{"email":"&"}/' * 

保留旧文件的副本

 sed -i.old 's/.*/{"email":"&"}/' * 

说明

  • -i.old修改文件而不是打印到stdout,并在使用扩展名修改之前保存每个文件的副本.old
  • s/old/news/old/new替换old
  • .*线上的任何字符
  • &匹配的模式

毫无疑问,更详细,但要编辑目录中的所有文件:

  1. 如果目录是平的:

     #!/usr/bin/env python3 import os import sys dr = sys.argv[1] for file in [os.path.join(dr, f) for f in os.listdir(dr)]: newtext = "\n".join(['{"email":"'+l.strip()+'"}'for l in open(file).readlines()]) open(file, "wt").write(newtext) 
  2. 如果目录是递归的,您还需要转换子目录中的文件:

     #!/usr/bin/env python3 import os import sys dr = sys.argv[1] for root, dirs, files in os.walk(dr): for file in files: file = os.path.join(root, file) newtext = "\n".join(['{"email":"'+l.strip()+'"}'for l in open(file).readlines()]) open(file, "wt").write(newtext) 

在这两种情况下,文件的内容都会变为:

 {"email":"%%%%%%%%%%@yahoo.com"} {"email":"%%%%%%@wanadoo.fr"} {"email":"%%%%raviplywoodglasscentre@yahoo.comravi"} {"email":"%%nameemail%%@yahoo.com"} {"email":"%.getincontact@numberland.com"} {"email":"%0dronbracale@roadrunner.com"} {"email":"%1%3@example.com"} {"email":"%1@elsewhere.com"} {"email":"%1@example.com"} 

使用它

  1. 将脚本(任一个)复制到一个空文件中,将其另存为edit_files.py
  2. 使用目录作为参数运行它:

     python3 /path/to/edit_files.py /path/to/files_to_convert 

注意

这假定需要编辑所有文件中的 所有行 。 如果我们需要为其中一个或两个设置条件,请提及。

使用awk ,使用变量赋值来删除一个引用级别:

 awk -v format='{"email":"%s"}\n' '{printf format, $1}' 

您可以使用流编辑器sed

 sed -e 's/\(^.*\)$/{"email":"\1"}/g' source.txt 

Perl方式:

 $ perl -lane 'print "{\"email\":\"$_\"}"' input.txt {"email":"%%%%%%%%%%@yahoo.com"} {"email":"%%%%%%@wanadoo.fr"} {"email":"%%%%raviplywoodglasscentre@yahoo.comravi"} {"email":"%%nameemail%%@yahoo.com"} 

这可以在文件夹中的多个文件上使用,如下所示:

 for file in * ; do perl -lane 'print "{\"email\":\"$_\"}"' "$file" > "$file".json ; done 

Python和json API:

 $ ls input2.txt input.txt json_encode.py* $ ./json_encode.py * $ ls input2.txt input2.txt.json input.txt input.txt.json json_encode.py* json_encode.py.json $ cat input.txt.json {"email": "%%%%%%%%%%@yahoo.com"} {"email": "%%%%%%@wanadoo.fr"} {"email": "%%%%raviplywoodglasscentre@yahoo.comravi"} {"email": "%%nameemail%%@yahoo.com"} 

这是剧本本身:

 #!/usr/bin/env python import json import sys for file in sys.argv[1:]: if __file__ in file or '.json' in file: continue with open(file,'r') as fd1: for line in fd1: data = { "email": line.strip() } with open(file+ ".json","a") as fd2: json.dump(data,fd2) fd2.write("\n")