linux - 循环内的 SED 命令

标签 linux bash loops sed do-loops

你好:我有很多文件叫test-MR3000-1.txttest-MR4000-1.nt,其中名称中的数字由100(即我有 11 个文件),

$ ls test-MR*
test-MR3000-1.nt  test-MR3300-1.nt  test-MR3600-1.nt  test-MR3900-1.nt
test-MR3100-1.nt  test-MR3400-1.nt  test-MR3700-1.nt  test-MR4000-1.nt
test-MR3200-1.nt  test-MR3500-1.nt  test-MR3800-1.nt

还有一个名为 resonancia.kumac 的文件,其中几行包含字符串 XXXX

$ head resonancia.kumac
close 0
hist/delete 0
vect/delete *
h/file 1 test-MRXXXX-1.nt
sigma MR=XXXX

我想执行一个 bash 文件,它用从命令 ls *MR* | 中获得的一组数字替换文件中的字符串 XXXX剪切-b 8-11

我找到了 a post其中有一些建议。我尝试自己的代码

for i in `ls *MR* | cut -b 8-11`; do
    sed -e "s/XXXX/$i/" resonancia.kumac >> proof.kumac
done

但是,在替换中,数字被单引号包围(例如 '3000')。

问:我应该怎么做才能避免数字集中出现单引号?谢谢。

最佳答案

这是描述环境的复制器:

for ((i=3000; i<=4000; i+=100)); do
  touch test-MR${i}-1.nt
done
cat >resonancia.kumac <<'EOF'
close 0
hist/delete 0
vect/delete *
h/file 1 test-MRXXXX-1.nt
sigma MR=XXXX
EOF

这是一个将在该环境中运行的脚本:

content="$(<resonancia.kumac)"
for f in *MR*; do
    substring=${f:7:3}
    echo "${content//XXXX/$substring}"
done >proof.kumac

...输出如下所示:

close 0
hist/delete 0
vect/delete *
h/file 1 test-MR300-1.nt
sigma MR=300

此输出中没有引号;描述的问题没有重现。

关于linux - 循环内的 SED 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17174144/

相关文章:

javascript - 如何使用 JavaScript 在 for in 循环中解析这个简单的 json 对象?

c++ - 限制 QWidget 大小

linux - 如何让 bash 子 shell 继承父级的设置选项?

linux - 如何使用 Bash 脚本拆分换行符?

bash - 从命令行在目录中查找文件

windows - 如何在 Windows 上通过 bash 访问 AWS SAM-CLI?

linux - Gnome 流光抓帧器混合

c++ - libGL 堆使用

java - 嵌套 while 循环在第一次迭代后停止?

c++ - 使用cstring数组c++循环获取用户输入