[文件使用sed命令消失[关闭]

问题描述 投票:-2回答:1
我正在编写一个简单的循环,我想在这些循环中更改初始的file.txt。当我运行脚本时,文件消失,并且在目标文件夹上创建了空白副本。

#!/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

这里有什么问题,我该如何在文本文件上标记变量?
linux bash shell gnu
1个回答
0
投票
注意上面[Tanmay Patil的评论,来自-

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了解详情,祝您好运。

© www.soinside.com 2019 - 2024. All rights reserved.