I'm modifying raw PCL/PS files (mixed) and for some reason I can't get my Sed syntax right for the true beginning that I want it to grab. Here's a sample output from the strings command:

%-12345X%!PS-Adobe-3.0 EPSF-3.0 <------Sed doesn't work for this pattern
%%Creator: tiff2ps  <----Sed works for this pattern

Here's my sed command that works:

sed -n '/%%Creator/,/%%EOF/p'

But I want it to start with %-12345X%:

sed -n '/%-12345X%/,/%%EOF/p'

When I do the last command, it just outputs the entire file. No combination from that line works. Now, I'm viewing the raw print file with 'strings', could it be that that line is encoded in a way that sed can't understand? Any idea on working around this?

Edited to add:

I'm pretty sure this has to do with the encoding of PCL and line escapes. The file goes from PCL to PS, and doesn't create the first message of PS on it's own line. Output of cat looks like this:

*c50BESC*c0PESC&f1XESC&f7y4XESC%-12345X%!PS-Adobe-3.0 EPSF-3.0
%%Creator: tiff2ps
  • I have tested it in "zsh" and "Bash" and it works. Which shell you use?
    – A.B.
    Commented Mar 31, 2015 at 17:24
  • 1
    Please edit your question and show us exactly what you're doing. What are you running strings on? What are you cating? What are you running the sed on? Is everything on a single line?
    – terdon
    Commented Mar 31, 2015 at 17:38

1 Answer 1


My guess is that sed is doing exactly what you're telling it to do: Print out the first "line" that contains %-12345X%. But since this is not an ASCII file, but a PCL or PDF file with all sorts of binary bytes in there- and no proper newline to speak of, until just in front of "%%Creator:"- it prints out the entire thing. Remember, sed prints the matching "line". What you're asking it to do, I think, is print "starting at this string".

If you want to take a file that's not guaranteed to be line-oriented (such as this), you're going to have to use a technique that does not depend on line-oriented tools. This may help: how to dump part of a binary file. It's a little more complicated but your strings are pretty distinctive so it should do the trick.

Hmm... just had a idea- maybe this would work. It deletes everything on the same "line" that's in front of the %-12345X% except for that string itself. Then it prints everything from that line to the end of file. I haven't tried this, but that's how I'd approach it:

sed -n -e '/%-12345X%/s/.*%-12345X%/%-12345X%/' -e '/%-12345X%/,/%%EOF/p'

Or even better:

sed -n -e "/${STR}/s/.*${STR}/${STR}/" -e "/${STR}/,/%%EOF/p"
  • Mike, I think you're on the right track. Let me test that.
    – RyanH
    Commented Mar 31, 2015 at 20:35
  • Mike, you're awesome! That worked perfectly. That saved me hours of Googling.
    – RyanH
    Commented Mar 31, 2015 at 20:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .