如何在Shell中的sed命令中使用变量 [英] How to use variable in sed command in shell

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

问题描述

我有以下代码

sed -i '' $line'i\
${String}\
' test.csv

其中 line = 3 string ='"a","b","c"'.

但是它没有按预期工作.它将字符串添加到每条备用行上.应该将字符串添加到第三行.如果我直接在命令中使用变量值,则相同的命令也可以正常工作.

But it doesn't work as expected. It adds the string on each alternate line. It is supposed to add the string on 3rd line. Same command works fine if I use the variable values directly in command.

此命令应在MacOSX上有效.(要在MacOSX上覆盖文件,必须使用 -i 表示法.)

This command should work on MacOSX. (The -i '' notation is necessary to overwrite the file on MacOSX.)

推荐答案

变量仅在不加引号(不建议)或用双引号引起来时才展开;它们不会在单引号内展开.

Variables are only expanded when unquoted (not recommended) or when enclosed in double quotes; they are not expanded inside single quotes.

当仅要转义一个或两个反斜杠时,将整个字符串放在双引号内,使反斜杠加倍,这是不合理的.如果您有任何反引号,也需要将其转义.如果您有多个反斜杠或反引号,那么通常最好使用单引号.

When there are but one or two backslashes to be escaped, it is not unreasonable to put the entire string inside double quotes, doubling up the backslashes. If you have any back-quotes, you need to escape those too. If you have more than a couple of backslashes or you have back-quotes, using single quotes generally becomes your better bet.

给出一个包含以下内容的数据文件 test.csv :

Given a data file test.csv containing:

bizarre,multiple,words
plain,simple,words
catastrophic,failure,of,imagination
pejorative,interjections,ignored
abelone,abyssinia,ablation
pokemon,harry,potter

因此,您可以通过以下方式实现所需的效果:

Thus, you can achieve the effect you want with:

$ line=3
$ string='"a","b","c"'
$ sed "${line}i\\
> ${string}
> " test.csv
bizarre,multiple,words
plain,simple,words
"a","b","c"
catastrophic,failure,of,imagination
pejorative,interjections,ignored
abelone,abyssinia,ablation
pokemon,harry,potter
$

$ {string} 之后的换行符是必需的.奇怪的是,您也可以在 $ {string} 之后添加一个或两个反斜杠,并获得相同的结果.或者,您可以用单引号将命令的选定部分引号,以确保变量不在单引号中,而是在双引号中

The newline after ${string} is required. Curiously, you can also add one or two backslashes after ${string} and get the same result. Or you can quote selected parts of the command in single quotes, making sure that variables are not in single quotes but are in double quotes:

$ sed "${line}"'i\
> '"$string"'
> ' test.csv
bizarre,multiple,words
plain,simple,words
"a","b","c"
catastrophic,failure,of,imagination
pejorative,interjections,ignored
abelone,abyssinia,ablation
pokemon,harry,potter
$

我没有使用 -i''选项来覆盖文件(创建不带任何扩展名的备份"),以便在标准输出(终端)上看到输出.您可以在macOS上添加这些选项以覆盖文件.Linux(GNU sed )上的相应表示法只是 -i 选项.您可以在两个平台上使用 -i.bak 来创建扩展名为 .bak 的备份.在Linux上, -i .bak 之间不能有空格,但是在macOS上允许该空间.因此, -i.bak 是唯一可移植的表示法,并且它创建了备份-并且可移植性仍然受到限制,因为其他Unix变体可能根本不支持 -i 选项.

I've not used the -i '' options to overwrite the file (creating a 'backup' without any extension) so that the output is seen on standard output — the terminal. You can add those options on macOS to overwrite the file. The corresponding notation on Linux (GNU sed) is just the -i option. You can use -i.bak on both platforms to create a backup with the extension .bak; on Linux, you cannot have a space between the -i and the .bak, but the space is allowed on macOS. Thus -i.bak is the only portable notation, and it creates the backup — and the portability is still limited as other Unix variants may not support the -i option at all.

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

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