Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3699212
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:04:32+00:00 2026-05-19T05:04:32+00:00

Why does this produce these results? C:\>(echo a && echo b) | sed 1!G;h;$p

  • 0

Why does this produce these results?

C:\>(echo a  &&  echo b) | sed "1!G;h;$p"
a
b
a
b
a

C:\>

Added-
I see now there is no question, that it’d give those results.. but

(added note- 2nd line = last line. But I see you wrote last line to emphasise that it’s $ matching the 2nd line as last line. I accept that notation. Also, 1st line, 2nd line, last line, refer to lines of input.)

Jonathan, you wrote
a – 1st line, pattern space
b – 2nd line, pattern space, line 1
a – 2nd line, pattern space, line 2
b – last line, $p pattern space, line 1
a – last line, $p pattern space, line 2

But wouldn’t it be
Note- Dennis’s comment has confirmed that the “But wouldn’t it be” is correct

a – 1st line, pattern space
b – last line, $p pattern space, line 1
a – last line, $p pattern space, line 2
b – 2nd line, pattern space, line 1
a – 2nd line, pattern space, line 2

i.e. the same output
a
b
a
b
a

But the descriptions of how it did the b a, is the other way around

That’s assuming the $ operates on the last line, and not after it..

But what you wrote makes it look like the $ operates after it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T05:04:33+00:00Added an answer on May 19, 2026 at 5:04 am
    • 1!G appends the hold space to the pattern space after the first line.
    • h copies the pattern space to the hold space (on every line).
    • $p prints the last line (again).

    The first input line is ‘a’; the G command is ignored; the line is copied to the hold space; the line is printed (because you didn’t say -n).

    The second input line is ‘b’; the G command appends the hold space (‘a’) to the pattern space (‘b’); the ‘h’ command copies the pattern space to the hold space; the pattern space is printed once because of the ‘no -n‘.

    There is no more input, so the $p acts and prints the pattern space.

    So, you get:

    • a – 1st line, pattern space
    • b – 2nd line, pattern space, line 1
    • a – 2nd line, pattern space, line 2
    • b – last line, $p pattern space, line 1
    • a – last line, $p pattern space, line 2

    The question is asked: did I tag the pairs of ‘b/a’ lines backwards?

    Good question: not sure…how would we find out?

    Let’s add another operation to the ‘$’ set:

    (echo a; echo b) | sed -e '1!G;h;$p;$s/b\na/X'
    

    The output is:

    a
    b
    a
    X
    

    which shows that the $p print does occur before the $s/// operation, which occurs before the final print of the pattern space.

    One side-effect of this observation, which caught me by surprise (but makes sense on reflection), is that sed knows when it is processing the last line as it is running the script on the last line. That means it does read-ahead after the newline to see whether there is more data to fetch. Dennis Williamson shows that the sed source at about line 928 contains a function test_eof() which does indeed do one character of lookahead.

    (One of the good things about SO is that you learn even as you teach!)

    So, somewhat to my surprise, it seems that sed knows when it has reached the last line before it processes it – so it seems to do some sort of read-ahead. Either that or I’ve misunderstood something really badly, or it’s too late and I need to go to sleep.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a self-explanatory question: Why does this thing bubble into my try catch's
Does anyone know if it is possible to write a query that can produce
Does this pattern: setTimeout(function(){ // do stuff }, 0); Actually return control to the
Why does this javascript return 108 instead of 2008? it gets the day and
What does this mean exactly? I'm doing something like this: File.Copy(@\\foo\bar\baz.txt, @c:\test\baz.txt); MSDN doesn't
Why does this lambda expression not compile? Action a = () => throw new
What does this option do? It is accessible from the Build menu.
Why does this test program result in a java.lang.IllegalMonitorStateException ? public class test {
Why does this code fail to display the category name Apples using the current
Why does this code: class A { public: explicit A(int x) {} }; class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.