Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • 1
    Note: In several sed implementations including the original one, :a; N; $!ba; s/\n//g' is to define a label called a; N; $!ba. Commented Oct 9 at 12:25
  • how does your sample input.txt differ from the actual file you want to modify? does it contain only two lines? if it contains more than two lines, do you want to merge all lines together? or join every second line to the previous line? or something else? do you need to insert a space or tab between the joined lines? Commented Oct 9 at 12:25
  • related, but not a dupe: Make multiple edits with a single call to sed Commented Oct 9 at 12:30
  • 1
    FWIW re: "I want to substitute - with _ and join the two lines" ... sed -z 's/-/_/g; s/\n//g' should do what you want at least with GNU sed. Commented Oct 9 at 14:56
  • 2
    Aside: sed is great for simple substiutions on individual lines but for anything else an awk script is usually some combination of clearer, simpler, more robust, more portable, etc. In this case awk -v ORS= '{gsub(/-/,"_"); print} END{if (NR) print RS}' input.txt would do what you want in any awk without relying on an arcane pile of single-character instructions and without reading all of the input into memory and so would work for arbitrarily large input. Commented Oct 10 at 13:24