开发者

How to replace or delete lines containing / character in BASH?

开发者 https://www.devze.com 2023-02-25 07:49 出处:网络
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

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 exa开发者_运维技巧mple 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消