【发布时间】:2014-04-08 04:44:31
【问题描述】:
我实际上是在尝试使用 sed 删除文本文档中的几行。把它清理干净。但我一点也不明白。遗漏了一些东西,我不知道是什么......
#!/bin/bash
items[0]='X-Received:'
items[1]='Path:'
items[2]='NNTP-Posting-Date:'
items[3]='Organization:'
items[4]='MIME-Version:'
items[5]='References:'
items[6]='In-Reply-To:'
items[7]='Message-ID:'
items[8]='Lines:'
items[9]='X-Trace:'
items[10]='X-Complaints-To:'
items[11]='X-DMCA-Complaints-To:'
items[12]='X-Abuse-and-DMCA-Info:'
items[13]='X-Postfilter:'
items[14]='Bytes:'
items[15]='X-Original-Bytes:'
items[16]='Content-Type:'
items[17]='Content-Transfer-Encoding:'
items[18]='Xref:'
for f in "${items[@]}"; do
sed '/${f}/d' "$1"
done
我在想的,似乎是错误的,我可以设置一个 for 循环来检查数组中我想从文本文件中删除的每个项目。但这根本行不通。任何想法。当然这是基本和简单的,但我无法弄清楚。
谢谢,
马雷克
【问题讨论】:
-
${f}不会在单引号中展开。你想要双引号。 -
此外,您希望在循环中保存对文件的更改,否则您最终会得到相同的文件。对
sed使用-i选项。 -
你为什么不使用比 sed 更快的
grep -v -f YourSuppressList YourSourceFile(在你的情况下 fgrep 仍然更好)?