Skip to main content
added 24 characters in body
Source Link
αғsнιη
  • 41.9k
  • 17
  • 75
  • 117

Using awk:

awk -F'-$' '{ printf "%s", sep $1; sep=/-$/?"":OFS } END{ print "" }' infile

with the -F'-$', we defined the Field Separator to single hyphen at the end of line, so with this and by taking the first field $1, we will always have the line without that hyphen for those line having this hyphen or still entire line for those not having that hyphen.

then we do simply printing it with a sep in between but that changes when reading the next line to empty-string if current line was ending with a hyphen otherwise to OFS (Output Field Separator, default is Space character).

at the END{...} block we are adding a final newline character to make it a POSIX text file, if you don't want that to be added, just remove that part.


Using sed, alternatively:

sed ':loop /-$/N;s/-\n//;t loop; N;s/\n/ /;t loop' infile
  • :loop
    • if a line ended with a hyphen (testing with /-$/), do read the Next line and replace the "hyphen+\newline" with an empty string.
      if substitution was successful (testing with t), then jump to the label loop and process the next line and skip executing rest of the code.
    • else, read the Next line and removereplace the embedded \newline in between those two lines with a space character.
      if substitution here was also successful, then jump to the label loop and process the next line.

Using awk:

awk -F'-$' '{ printf "%s", sep $1; sep=/-$/?"":OFS } END{ print "" }' infile

with the -F'-$', we defined the Field Separator to single hyphen at the end of line, so with this and by taking the first field $1, we will always have the line without that hyphen for those line having this hyphen or still entire line for those not having that hyphen.

then we do simply printing it with a sep in between but that changes when reading the next line to empty-string if current line was ending with a hyphen otherwise to OFS (Output Field Separator, default is Space character).

at the END{...} block we are adding a final newline character to make it a POSIX text file, if you don't want that to be added, just remove that part.


Using sed, alternatively:

sed ':loop /-$/N;s/-\n//;t loop; N;s/\n/ /;t loop' infile
  • :loop
    • if a line ended with a hyphen (testing with /-$/), do read the Next line and replace the "hyphen+\newline" with an empty string.
      if substitution was successful (testing with t), then jump to the label loop and process the next line and skip executing rest of the code.
    • else, read the Next line and remove \newline in between those two lines.
      if substitution here was also successful, then jump to the label loop and process the next line.

Using awk:

awk -F'-$' '{ printf "%s", sep $1; sep=/-$/?"":OFS } END{ print "" }' infile

with the -F'-$', we defined the Field Separator to single hyphen at the end of line, so with this and by taking the first field $1, we will always have the line without that hyphen for those line having this hyphen or still entire line for those not having that hyphen.

then we do simply printing it with a sep in between but that changes when reading the next line to empty-string if current line was ending with a hyphen otherwise to OFS (Output Field Separator, default is Space character).

at the END{...} block we are adding a final newline character to make it a POSIX text file, if you don't want that to be added, just remove that part.


Using sed, alternatively:

sed ':loop /-$/N;s/-\n//;t loop; N;s/\n/ /;t loop' infile
  • :loop
    • if a line ended with a hyphen (testing with /-$/), do read the Next line and replace the "hyphen+\newline" with an empty string.
      if substitution was successful (testing with t), then jump to the label loop and process the next line and skip executing rest of the code.
    • else, read the Next line and replace the embedded \newline in between those two lines with a space character.
      if substitution here was also successful, then jump to the label loop and process the next line.
added explanation
Source Link
αғsнιη
  • 41.9k
  • 17
  • 75
  • 117

Using awk:Using awk:

awk -F'-$' '{ printf "%s", sep $1; sep=/-$/?"":OFS } END{ print "" }' infile

Or usingwith the sed-F'-$':, we defined the Field Separator to single hyphen at the end of line, so with this and by taking the first field $1, we will always have the line without that hyphen for those line having this hyphen or still entire line for those not having that hyphen.

then we do simply printing it with a sep in between but that changes when reading the next line to empty-string if current line was ending with a hyphen otherwise to OFS (Output Field Separator, default is Space character).

at the END{...} block we are adding a final newline character to make it a POSIX text file, if you don't want that to be added, just remove that part.


Using sed, alternatively:

sed ':aloop /-$/N;s/-\n//;ta;;t loop; N;s/\n/ /;ta';t loop' infile
  • :loop
    • if a line ended with a hyphen (testing with /-$/), do read the Next line and replace the "hyphen+\newline" with an empty string.
      if substitution was successful (testing with t), then jump to the label loop and process the next line and skip executing rest of the code.
    • else, read the Next line and remove \newline in between those two lines.
      if substitution here was also successful, then jump to the label loop and process the next line.

Using awk:

awk -F'-$' '{ printf "%s", sep $1; sep=/-$/?"":OFS } END{ print "" }' infile

Or using sed:

sed ':a /-$/N;s/-\n//;ta; N;s/\n/ /;ta' infile

Using awk:

awk -F'-$' '{ printf "%s", sep $1; sep=/-$/?"":OFS } END{ print "" }' infile

with the -F'-$', we defined the Field Separator to single hyphen at the end of line, so with this and by taking the first field $1, we will always have the line without that hyphen for those line having this hyphen or still entire line for those not having that hyphen.

then we do simply printing it with a sep in between but that changes when reading the next line to empty-string if current line was ending with a hyphen otherwise to OFS (Output Field Separator, default is Space character).

at the END{...} block we are adding a final newline character to make it a POSIX text file, if you don't want that to be added, just remove that part.


Using sed, alternatively:

sed ':loop /-$/N;s/-\n//;t loop; N;s/\n/ /;t loop' infile
  • :loop
    • if a line ended with a hyphen (testing with /-$/), do read the Next line and replace the "hyphen+\newline" with an empty string.
      if substitution was successful (testing with t), then jump to the label loop and process the next line and skip executing rest of the code.
    • else, read the Next line and remove \newline in between those two lines.
      if substitution here was also successful, then jump to the label loop and process the next line.
added 71 characters in body
Source Link
αғsнιη
  • 41.9k
  • 17
  • 75
  • 117

Using awk:

awk -F'-$' '{ printf "%s", sep $1; sep=/-$/?"":OFS } END{ print "" }' infile

Or using sed:

sed ':a /-$/N;s/-\n//;ta; N;s/\n/ /;ta' infile

Using awk:

awk -F'-$' '{ printf "%s", sep $1; sep=/-$/?"":OFS } END{ print "" }' infile

Using awk:

awk -F'-$' '{ printf "%s", sep $1; sep=/-$/?"":OFS } END{ print "" }' infile

Or using sed:

sed ':a /-$/N;s/-\n//;ta; N;s/\n/ /;ta' infile
added 10 characters in body
Source Link
αғsнιη
  • 41.9k
  • 17
  • 75
  • 117
Loading
Source Link
αғsнιη
  • 41.9k
  • 17
  • 75
  • 117
Loading