替换所有文件的字符串 - 的Unix [英] Replace a string in all files - Unix

查看:126
本文介绍了替换所有文件的字符串 - 的Unix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图替换字符串 ::: 在一批txtfiles的所有行(它可以被认为是一个字,因为总是有一个在前面和后面的空间中。

我可以像下面蟒蛇做,但有一个不太过度杀伤/错综复杂通过UNIX终端这样的方式?(允许多管)

 下载=./td/
OUTDIR ='./od/
在glob.glob infile中(os.path.join(下载,*)):
  _,= FILENAME os.path.split这样的()
  对于L在codecs.open(INFILE,'R','UTF8')readlines方法():
    L = l.replace(:::,::)。带()
    OUTFILE = codecs.open(os.path.join(OUTDIR,文件名),'A +','UTF8')
    打印>> OUTFILE,L

然后我将所有文件从OD到TD MV ./od/* ./td / *


解决方案

 找到。 -name./td/*.c-exec SED -iS / ::: / :: / G'{}'\\;

无需 OD / 的。

编辑:

一个稍微简单一些变化:

  LS TD / * C。| xargs的sed的-i'S / ::: / :: / G

I am trying to replace a string ::: with :: for all lines in a batch of txtfiles (it can be considered as a word since there's always a space in front and behind it.

I can do it with python like below, but is there a less 'over-kill' / convoluted way of doing this through the unix terminal? (Many pipes allowed)

indir = "./td/"
outdir =  './od/'
for infile in glob.glob(os.path.join(indir,"*")):
  _,FILENAME = os.path.split()
  for l in codecs.open(infile,'r','utf8').readlines():
    l = l.replace(":::","::").strip()
    outfile = codecs.open(os.path.join(outdir,FILENAME),'a+','utf8')
    print>>outfile, l

Then i move all files from od to td mv ./od/* ./td/*

解决方案

find . -name "./td/*.c" -exec sed -i "s/:::/::/g" '{}' \;

No need for od/ at all.

EDIT:

A slightly simpler variation:

ls td/*.c | xargs sed -i '' "s/:::/::/g"

这篇关于替换所有文件的字符串 - 的Unix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆