#!/bin/bash
for i in {1..9..1}
do
k=$(( 10 - $i ))
c_2=`echo "0.1 * $i" | bc`
c_1=`echo "0.1 * $k" | bc`
sed 's/c_1/c_1/g' file.txt > file${c_1}${c_2}.txt
sed 's/c_2/c_2/g' file.txt > file${c_1}${c_2}.txt
mkdir `pwd`/folder${c_1}${c_2}
mv `pwd`/file${c_1}${c_2}.txt `pwd`/folder${c_1}${c_2}
done
这里有什么问题,我该如何在文本文件上标记变量?
sed 's/c_1/c_1/g' file.txt > file${c_1}${c_2}.txt # creates file
sed 's/c_2/c_2/g' file.txt > file${c_1}${c_2}.txt # destroys and replaces previous content
to-
sed 's/c_1/c_1/g' file.txt > file${c_1}${c_2}.txt # creates file with change #1 only sed 's/c_2/c_2/g' file.txt >> file${c_1}${c_2}.txt # creates 2nd copy of content with change #2 only
首先,您有单引号并且没有嵌入变量,因此它不会做任何事情。请参见下面的双引号版本;但除此之外...他们建议的更正(
>
至>>
)可能会或可能不会做您想要的;这将包括第一份副本中第2个sed
的所有原始,未更改的内容,以及第二份副本中第1个sed
的所有原始,未更改的内容。这似乎并不是您想要的。最好使用Aaron-]注释的组合编辑>
sed "s/c_1/$c_1/g;s/c_2/$c_2/g" file.txt > "newFile${c_1}${c_2}.txt" # all edits in one pass
这将在同一遍中对文件进行两组更改,并输出数据的单个副本,包括所有编辑,没有冗余以及受影响行的原始版本。c.f。 the sed manual了解详情,祝您好运。