在sed命令的Bash变量 [英] Bash variable in sed command

查看:144
本文介绍了在sed命令的Bash变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据该文件的名称递归添加头几个文件

I need to add an header recursively to several file according to the name of the file.

所以,我曾尝试:

for i in *file 
do     
sed -i '1 i \A;B;C;D;E;F;G;H;${i%??}' a_${i} > header_a_${i}
done

问题是,可变反射的文件的名称不扩大,并在标头我有$ {则i%??}而不是名称文件的一部分(%??是去除一些结束字符)。

the problem is that the variable reflecting the name of the file does not expand and in the header I have ${i%??} instead of part of the name file (%?? is to remove some ending characters).

任何帮助将是巨大的。

Any help would be great.

推荐答案

使用双引号:

    sed '1 i\
A;B;C;D;E;F;G;H;'"${i%??}" a_${i} > header_a_${i}

它没有任何意义,使用-i将输出重定向,所以我省略了-i。此外,我已经添加了插入命令后逃脱换行符。有些SED不需要换行,但很多人。但是,它似乎很奇怪使用sed这一点。相反,只是做:

It doesn't make any sense to use -i and to redirect the output, so I've omitted -i. Also, I've added an escaped newline after the insert command. Some sed do not require the newline, but many do. However, it seems odd to use sed for this. Instead, just do:

for i in *file; do     
  { echo "A;B;C;D;E;F;G;H;${i%??}"; cat a_${i}; } > header_a_${i}
done

这篇关于在sed命令的Bash变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆