+ -
当前位置:首页 → 问答吧 → 将一个目录下的所有文件内容里的某个字符串a更改为字符串b的实现

将一个目录下的所有文件内容里的某个字符串a更改为字符串b的实现

时间:2011-09-03

来源:互联网

#!/bin/bash
#Program:        This program is to replace the specific string within documents in a dir
#History:        2011-09-03        wfc        First Release
dir_in=/home/lasg/work ###################################
cd $dir_in
# 1) get the names of these documents in the dir
names=`ls -l | cut -d ' ' -f 8` ; echo "the documents are:${names}"
echo "-----------------------------------------------------------"
# 2)replace the specific string and > to new document
dir_out=/home/lasg/work2  ##########
sudo mkdir -p $dir_out
# defaults: please make sure only documents in dir,if not, you may do it like this:
#        during 1) the names are printed on the screen, you may copy them to here
# for name in `here` ######### like: for name in `doc1 doc2 doc3`
# and add # to the line below
for name in ${names} ###########################
do
  new=$name; echo $new
  touch $dir_out/$new
  # this is an example: replace 'o' with 'oo'#######################
  cat $name |sed 's/o/oo/g'> $dir_out/$new ;
  unset new
echo "-----------------------------------------------------------"
done

作者: wfc1102   发布时间: 2011-09-03

路过

作者: Goando   发布时间: 2011-09-03