0
sed -nE "s/(IMAGE)(.*)/\1\2/p" somefile > sfh.raw

somefile contains random ASCII as well as binary data after the image. The above sed command works, if there is no newline binary data in the file. If there is a newline it just outputs only until the new line, ignoring the rest of the file.

Is there a way we can make sed (.*) capture everything including the new line and continue until the end of the somefile content.

IMAGE254656
   dsfdfdl;flkdfldsfkdsfkdlsfdfldfkdsfo;dsfkldsfdsfsd
4
  • Try grep -Eoz 'IMAGE.*' somefile > sfr.raw (if you have a GNU grep) Dec 10, 2018 at 14:04
  • 3
    sed 's/(foo)(bar)/\1\2/' is pointless since it is an identity transformation.
    – hek2mgl
    Dec 10, 2018 at 14:05
  • 1
    sed operates line by line, I do not think you can make it work across lines, or read the file as binary.
    – kabanus
    Dec 10, 2018 at 14:07
  • sed can work across lines; it is not designed to work with binary data.
    – Beta
    Dec 10, 2018 at 19:28

1 Answer 1

0

Consider using awk for this:

awk '/^IMAGE/{i=1}i' somefile

awk processes a file line by line and allows you to set variables and check contents and lots of other very fancy stuff for each line.

This script checks each line to see if it starts with IMAGE. If so, it sets variable i to 1. Then it checks to see if i is set. If so, it does its default behavior of printing the line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.