1

I'm trying to use sed to remove the rest of a line after: HTTP1.1" 200

I can't figure out how to get sed to understand I want that whole thing as a string to match, including the double quote and the space.

An example for good measure, I want to turn:

"GET /images/loading.gif HTTP/1.1" 200 10819 "https://...

into

"GET /images/loading.gif HTTP/1.1" 200
  • Well a lot of searching didn't help me find it. Do you feel comments like this are helping the community? – erds Feb 19 '16 at 18:31
  • Unfortunately as per you're example, you've fundamentally not understood my question. The key information I'd needed was how to sed the spaces. What it turned out to be was that i was also flipping the order for use of my quotes and double quotes. – erds Mar 8 '16 at 17:40
  • Comments removed buddy, what a shame you could not receive or accept multiple opinions. Have a nice day. – tachomi Mar 8 '16 at 17:49
5

You have to quote strings with spaces when you use sed (or most other tools) from the commandline. And since you already use the double quote, you have to go for single quotes:

echo '"GET /images/loading.gif HTTP/1.1" 200 10819 "https://...' | \
     sed 's|HTTP/1.1" 200.*|HTTP/1.1" 200|'

gives:

"GET /images/loading.gif HTTP/1.1" 200
1

With GNU grep:

| grep -o '.*HTTP/1\.1" 200'

With GNU sed:

| sed -r 's/(.*HTTP\/1\.1" 200).*/\1/'
  • Is the (?= ) lookahead necessary? I think you can remove it and -P. – Digital Trauma Feb 13 '16 at 0:20
1

If you just append a newline delimiter after your matched string you can Print only so much of pattern space without having to modify it overmuch. This can usually even work when pattern space contains bytes which are not parts of a character.

sed -n 's|HTTP/1.0” 200|&\n|;P' <in >out

note: portably you'll want to use a literal newline character in place of the n used above, though it does work as written in a GNU sed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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