2

The sed command 'H' appends from patternspace to holdspace after appending a new line into the holdspace first.

How can I append without inserting a new line into holdspace?

MoN
  • 53
  • 4
  • Any time you find yourself taking about holdspace or patternspace in sed you are using the wrong tool as that functionality became obsolete in the mid 1980s when awk was invented. sed is for s/old/new, that is all, and the only relevant commands are s, g, and p (with -n). If you add a [mcve] with concise, testable sample input and expected output and tag your question with awk then we can show you how to do whatever it is you want to do the right way. – Ed Morton Jun 12 '18 at 08:59
  • @EdMorton how to use g command if don't consider holdspace? I think sed could be the right tool in some situations, not only with s/old/new/, maybe not for this. – Hazzard17 Jun 12 '18 at 09:29
  • @Hazzard17 why would you need to talk about holdspace to use `g`? I assume you're saying that internally `g` uses "holdspace" for something. OK, there's absolutely no reason for anyone to know/care/talk about it. No, there are no other situations where sed is the right tool - whatever the job is an awk solution will always be some combination of clearer, simpler, more efficient, more portable, easier to enhance/maintain, etc. Even IF there we some niche case where a sed solution was somehow "better" it'd be just that, a niche case, and you'd be better off learning/expanding your awk knowledge – Ed Morton Jun 12 '18 at 09:59
  • @Hazzard17 if you'd like to post a new question with a problem that requires a sed solution that uses constructs other than s, g, and p then I'd be happy to post an awk solution to the same problem for you to compare. – Ed Morton Jun 12 '18 at 10:07
  • @Ed Morton Thanks for the hint, I have found a solution to my problem already using awk. But the question remains - if it is possible using sed or not? – MoN Jun 12 '18 at 10:15
  • @EdMorton ok, I think you are meaning the `g` flag applicable to the `s` command, not the `g` command that "replace the contents of the pattern space with the contents of the hold space". About the use of sed vs awk or others tools, I afraid this is basically your point of view. – Hazzard17 Jun 12 '18 at 10:21
  • 1
    @MoN I think that couldn't be done, at least with one single command. – Hazzard17 Jun 12 '18 at 10:24
  • @Hazzard17 Thanks for the answer. – MoN Jun 12 '18 at 10:29
  • @Hazzard17 correct, based on my experience of 35+ years using sed and 25+ years using awk, that is my point of view. – Ed Morton Jun 12 '18 at 11:58

1 Answers1

2

There are some techniques and it depends on your goals. For example, you can use h for the first line and remove the new line after each H or at the end of the input ...

Removing for each append:

seq -w 10 | sed -n 'H;x;s/\n//g;x;${x;p}'

Removing at the end:

seq -w 10 | sed -n '1h;1!{H};${x;s/\n//g;p}'

The trick is using x (exchange the contents of the hold and pattern spaces) and remove the newline.