我在目录结构中有一组文件,比如
/tmp1/folder1/file1
/tmp1/folder2/file2
现在我想要从这两个文件中获取一些行&在类似于上面的路径中创建/覆盖文件,使文件中的行转到相应的文件夹
/tmp2/folder1/grep_from_file1
/tmp2/folder2/grep_from_file2
答案 0 :(得分:1)
您可以使用此脚本:
for f in tmp1/folder1/file[12]
do
t=tmp2/"${f#*/}"
t="${t/\///grep_from_}"
grep "search pttern" "$f" > "$t"
done
答案 1 :(得分:1)
这样的事情应该达到目的:
#!/bin/bash -f
originalFiles=("/tmp1/folder1/file1" "/tmp1/folder2/file2") # Add more
for file in "${originalFiles[@]}"
do
newDirName=`dirname $file | sed s/tmp1/tmp2/`
newFileName=`basename $file | sed s/^/grep_from_/`
mkdir -p $newDirName
grep "text_to_grep" $file > $newDirName/$newFileName
done
答案 2 :(得分:1)
用grep命令中的期望关键字替换“KEY”:
#! /usr/bin/bash
source=/tmp1
dest=/tmp2
find $source -type f |while read file
do
fold=${file/$source/$dest}
fold=${fold%/*}
name=${file##*/}
mkdir -p $fold
grep "KEY" $file > $fold/grep_from_$name
done