Search code examples
bashshellgrepsh

Grep: Issue with null byte


I have the following command in a bash script.

GREP_RESULT=$(grep -zoP "\.global LIST(.|\n)*-1\Z" part1.s)

I'm getting the following error.

warning: command substitution: ignored null byte in input

I have tried the solution proposed in this post, changing it to the following.

GREP_RESULT=$(tr -d '\0' < grep -zoP "\.global LIST(.|\n)*-1\Z" part1.s)

This in turn results to the following error.

grep: No such file or directory

What is the proper way to work around the first error?


Solution

  • UPDATE:

    Comment from OP: grep -z is adding a \0 on end of stream

    So, this is a case of needing to modify the grep output (as opposed to modifying the input file):

    grep -zoP "\.global LIST(.|\n)*-1\Z" part1.s | tr -d '\0'
    

    Original answer ...

    Apply the tr to the data file first; a couple variations:

    grep -zoP "\.global LIST(.|\n)*-1\Z" <(tr -d '\0' < part1.s)
    
    # or 
    
    tr -d '\0' < part1.s | grep -zoP "\.global LIST(.|\n)*-1\Z"