通过shellscript编辑文件?

时间:2020-05-29 16:11:33

标签: bash awk windows-subsystem-for-linux

我需要解压缩文件,并且对于该文件中的每个文档,我需要对注释进行更正(文件以C编写)。我对如何对文件中的每个文档执行此操作感到困惑(我不知道文档的数量或名称),是否可以在文件夹中编写for循环以打开每个文档单独编写文档,然后在shellscript中对其进行编辑?

我对sed和awk之间的区别也有点困惑,我应该在这里使用哪个。我需要编辑注释(以//开头),并认为我应该使用awk,但不太确定。

我到目前为止的伪代码就是这样

unzip $1
edit_comment(){
    cat file1 file2 ... filen > total.txt
    *some awk command that will search for all instances of // and replace the text on that line (after 
    the //) with a new comment*
}

谢谢!

1 个答案:

答案 0 :(得分:0)

我相信您正在寻找How to do a recursive find/replace of a string with awk or sed

假设:
-参数$1是一些folder.tar.gzarchive.zipblah.bz4
-该未压缩存档中包含注释的所有文件都应更改

dir=$(echo $1 | sed 's#\..*##')
find $dir -type f -print0 | xargs -0 sed -i 's#//old comment #//new comment#'