如何替换或删除 BASH 中包含 / 字符的行?
我有一个以一些文本行开头并以许多二进制行继续的文件。我需要从开头删除文本行以获得纯二进制内容。假设二进制部分以特殊单词开头,例如:Connection。我尝试使用带有 sed
的 bash 脚本来删除所有这些文本行。问题是:包含 /
字符的文本行导致 sed
将输入中 /
之后的字符解释为单独的命令。我的代码是这样的:
while read line
do
sed -i "/"$line"/d" file_name
if [ "$line" != Connection* ]
break;
done < file_name
这样,当变量“line”包含 /
字符(例如 css/jj)时,sed 返回错误:
sed:-e 表达式 #1,字符 6:未知命令:`j'
如何将包含 /
字符的变量传递给 sed
来删除相关行? 顺便问一下,有没有办法将文件中的二进制内容与文本分开?有时我的文件在二进制部分的开头没有任何特殊的单词或符号来识别它。
I have a file that starts with some text lines and continues with many binary lines. I need to delete text line from its beginning to gain a pure binary content. Assume that binary section begins with a special word like: Connection. I tried to do this using a bash script with sed
to delete all those text lines. The problem is: text lines containing /
character cause sed
to interpret character after /
in input as a separate command. My code is like this:
while read line
do
sed -i "/"$line"/d" file_name
if [ "$line" != Connection* ]
break;
done < file_name
this way, when variable "line" contains /
character (for example css/jj) sed returns an error :
sed: -e expression #1, char 6: unknown command: `j'
How can I pass variables containing /
character to the sed
to delete related lines?
By the way, is there a way to separate binary content from text in a file? Sometimes my files don't have any special word or sign at beginning of binary section to recognize it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以安全地将
/
分隔符替换为任何其他字符,前提是您在每次出现时都替换它。有时,我使用§
,这在大多数文本中并不常见。You can safely replace
/
delimiter by any other character, provided that you replace it in every occurrence, Sometimes, I use§
which is quite uncommon in most texts.