嗨我需要编辑一些文件,但我不想手动操作,我知道使用sed命令我可以使用命令行编辑文件,但在这种情况下,我不知道如何匹配模式编辑。例如,我有这个文件:
(
AMI1
{
type patch; // <- relpace patch by cyclicAMI;
nFaces 1350;
startFace 2433406;
}
inlet
{
type patch;
nFaces 1125;
startFace 2434756;
}
outlet
{
type patch;
nFaces 1125;
startFace 2435881;
}
AMI2
{
type patch; // <- relpace patch by cyclicAMI;
nFaces 2850;
startFace 2440606;
}
)
我想编辑仅 AMI键,如下所示:
(
AMI1
{
type cyclicAMI; // <-- Replaced
inGroups 1(cyclicAMI); // <-- Add
nFaces 1350;
startFace 2433406;
matchTolerance 0.0001; // <-- Add
transform noOrdering; // <-- Add
neighbourPatch AMI2; // <-- Add AMI2 in AMI1
}
inlet
{
type patch;
nFaces 1125;
startFace 2434756;
}
outlet
{
type patch;
nFaces 1125;
startFace 2435881;
}
AMI2
{
type cyclicAMI; // <-- Replaced
inGroups 1(cyclicAMI); // <-- Add
nFaces 2850;
startFace 2440606;
matchTolerance 0.0001; // <-- Add
transform noOrdering; // <-- Add
neighbourPatch AMI1; // <-- Add AMI1 in AMI2
}
)
答案 0 :(得分:1)
不是太优雅,而是工作。
state=0
while IFS= read -r line; do
case "${line// }" in
AMI[12]) state=${line##*AMI}
echo "$line";;
typepatch\;*) echo " type cyclicAMI;"
echo " inGroups 1(cyclicAMI);";;
else
echo "$line"
fi;;
\}) if [ "$state" != 0 ]; then
echo " matchTolerance 0.0001;"
echo " transform noOrdering;"
echo " neighbourPatch AMI$((3-state));"
echo " }"
state=0
else
echo " }"
fi;;
*) echo "$line"
esac
done < textfile.txt