linux - 如何在 linux bash shell 中定义 $i?
问题描述
我在使用 bash shell 时遇到问题。这是我的linux命令代码:
for i in `cat linshi`;do sed -i '/$i/d' a.txt;done
linshi的内容是:
aa
bb
a.txt的内容是:
aa:wwersdf12314231234
bb:weorpius2345234523
cc:ertoiu230498234098
dd:234092834asdfkdfkg
我想删除a.txt的第一行和第二行。
但不幸的是,我发现 '/$i/d' 不正确。我试过'/\$i/d'和'/"\"$id/',但它们又失败了。谁能帮我?
解决方案
不要使用单引号,而是使用双引号。''
不进行任何变量扩展,但双引号会。
这将起作用:
for i in $(cat linshi);do sed -i "/$i/d" a.txt;done
推荐阅读
- pandas - 模式返回异常:必须产生聚合值
- vb.net - 列表视图没有给出数据库中的确切值
- sql-server - SSRS - 单个文本框中的行间距
- c# - 带有 ASP.NET Web 窗体的 Azure CosmosDB SDK v3
- go - 无法将复合文字与 map[string][]struct 一起使用
- java - 如何从包含税表的文件中读取特定的应税收入和收入税?
- awk - 如何打印来自不同文件的列?
- nginx-reverse-proxy - 是否可以用 OpenResty 在 Nginx Stream 前面实现 OIDC?
- php - Laravel 资源集合 - 分页 - JSON 响应错误
- php - 在 if 语句中使用 in_array 更快或更有效的替代方法