之前,我写了一个小的shell脚本来" retokenize"一个文件(用于比较健全性检查)。我目前需要为文件夹而不是仅仅一个文件做类似的事情。
我很好奇是否有一种简单的方法可以将以下内容重写为方法/函数,以及如何递归地将文件夹中的所有文件传递给方法,以便最终结果是所有文件都在该文件夹是" retokenized"。希望看看是否有一种快速简便的方法来做到这一点。正在做一些谷歌搜索和游戏,但想看看这里是否有人有一个快速/简单/清洁的解决方案。
一个文件的工作版本:
#!/bin/bash
date
outputDump="output.txt"
prodPropsFile="input.properties"
prodPropsSortedFile="sorted.properties"
tempPropsFile="temp.properties"
echo "Removing comments and empty lines from prod properties file"
sed '/^#/d' < $prodPropsFile > $tempPropsFile
sed '/^s*$/d' < $tempPropsFile > $prodPropsSortedFile
cp $prodPropsSortedFile $tempPropsFile
echo "Sorting prod properties by value length. So don't do double tokenization"
awk -F"=" '{ st = index($0,"="); print length(substr($0,st+1)),$0 }' $tempPropsFile | sort -rn | cut -d" " -f2- > $prodPropsSortedFile
echo "Retokenizing."
while IFS== read k v;
do
# Sed escape /, \, and &. Needed for urls like jdbc connections, etc.
escapedV=$(echo $v | sed -e 's/\\/\\\\/g; s/\//\\\//g; s/&/\\\&/g')
# The /gI will replace the tokens globally with case insensitive, this is important in case someone does something like "http://..." versus "HTTP://...".
sed -i -- "s/$escapedV/$k/gI" $outputDump;
done < "$prodPropsSortedFile"
示例属性文件:
%%token1%%=value1
%%token2%%=value2
示例输入文件:
This is a file that has value1 and value2.
示例输出文件:
This is a file that has %%token1%% and %%token2%%.
答案 0 :(得分:0)
更新了适用于Mac上文件夹中所有文件的脚本:
#!/bin/bash
date
retokenize()
{
echo "Retokenizing $file"
while IFS== read k v;
do
# Sed escape /, \, and &. Needed for urls like jdbc connections, etc.
escapedV=$(echo $v | sed -e 's/\\/\\\\/g; s/\//\\\//g; s/&/\\\&/g')
sed -i '' "s/$escapedV/$k/g" $file;
done < "$prodPropsSortedFile"
}
# Copy our input to a output file that we will modify, so we don't affect the original.
inputDump="IIQExports"
prodPropsFile="input.properties"
prodPropsSortedFile="sorted.properties"
tempPropsFile="temp.properties"
echo "Removing comments and empty lines from prod properties file"
sed '/^#/d' < $prodPropsFile > $tempPropsFile
sed '/^s*$/d' < $tempPropsFile > $prodPropsSortedFile
cp $prodPropsSortedFile $tempPropsFile
echo "Sorting prod properties by length."
awk -F"=" '{ st = index($0,"="); print length(substr($0,st+1)),$0 }' $tempPropsFile | sort -rn | cut -d" " -f2- > $prodPropsSortedFile
echo "Retokenizing."
find ./$inputDump/ -type f > foo.txt
IFS=$'\n';for file in $(cat foo.txt);
do
retokenize $file;
done
echo "Done."
date