Skip to main content
added 463 characters in body
Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k

Bonus ed content:

$ printf '%s\n' 'g/^module/ j' ,p Q | ed -s file
module  x(a,b,c)
module  y(d,e,f,
g,h,i)
module  z(j,k,l)

The above joins any line starting with the string module with its next line.

$ printf '%s\n' 'v/^module/ -,.j' ,p Q | ed -s file
module  x(a,b,c)
module  y(d,e,f,  g,h,i)
module  z(j,k,l)

The above joins any line not starting with the string module with its previous line.


Bonus ed content:

$ printf '%s\n' 'g/^module/ j' ,p Q | ed -s file
module  x(a,b,c)
module  y(d,e,f,
g,h,i)
module  z(j,k,l)

The above joins any line starting with the string module with its next line.

$ printf '%s\n' 'v/^module/ -,.j' ,p Q | ed -s file
module  x(a,b,c)
module  y(d,e,f,  g,h,i)
module  z(j,k,l)

The above joins any line not starting with the string module with its previous line.

added 8 characters in body
Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k

What you want to do is to join the module lines with the next line.

Using sed:

$ sed '/^module/N;s/\n//' file
module  x(a,b,c)
module  y(d,e,f,
g,h,i)
module  z(j,k,l)

This is with your data copied and pasted as is, with spaces at the end of each line.

The sed command will print each line as it is read, but when it encounters a line that starts with the string module, it appends the next line with an embedded newline character in-between (this is what N does). We remove that newline character with a substitution before the result is printed.

If your data has no spaces at the end of the lines, use

$ sed '/^module/N;s/\n/ /' file
module x(a,b,c)
module y(d,e,f,
g,h,i)
module z(j,k,l)

Just in case you'd want this (assuming no spaces at end of input lines):

$ sed -e '/^module/bpp' -e 'H;$bpp' -e 'd' \
      -e ':pp' -e 'x;/^$/d;s/\n/ /g' file
module x(a,b,c)
module y(d,e,f, g,h,i)
module z(j,k,l)

Annotated sed script:

/^module/ b print_previous; # print previous record
H;                          # append this line to hold space
$         b print_previous; # print previous (last) record
d;                          # end processing this line

:print_previous;            # prints a record accumulated in the hold space
x;                          # swap in the hold space
/^$/ d;                     # if line is empty, delete it
s/\n/ /g;                    # delete allreplace embedded newlines by spaces
                            # (implicit print)

What you want to do is to join the module lines with the next line.

Using sed:

$ sed '/^module/N;s/\n//' file
module  x(a,b,c)
module  y(d,e,f,
g,h,i)
module  z(j,k,l)

This is with your data copied and pasted as is, with spaces at the end of each line.

The sed command will print each line as it is read, but when it encounters a line that starts with the string module, it appends the next line with an embedded newline character in-between (this is what N does). We remove that newline character with a substitution before the result is printed.

If your data has no spaces at the end of the lines, use

$ sed '/^module/N;s/\n/ /' file
module x(a,b,c)
module y(d,e,f,
g,h,i)
module z(j,k,l)

Just in case you'd want this (assuming no spaces at end of input lines):

$ sed -e '/^module/bpp' -e 'H;$bpp' -e 'd' \
      -e ':pp' -e 'x;/^$/d;s/\n//g' file
module x(a,b,c)
module y(d,e,f, g,h,i)
module z(j,k,l)

Annotated sed script:

/^module/ b print_previous; # print previous record
H;                          # append this line to hold space
$         b print_previous; # print previous (last) record
d;                          # end processing this line

:print_previous;            # prints a record accumulated in the hold space
x;                          # swap in the hold space
/^$/ d;                     # if line is empty, delete it
s/\n//g;                    # delete all embedded newlines
                            # (implicit print)

What you want to do is to join the module lines with the next line.

Using sed:

$ sed '/^module/N;s/\n//' file
module  x(a,b,c)
module  y(d,e,f,
g,h,i)
module  z(j,k,l)

This is with your data copied and pasted as is, with spaces at the end of each line.

The sed command will print each line as it is read, but when it encounters a line that starts with the string module, it appends the next line with an embedded newline character in-between (this is what N does). We remove that newline character with a substitution before the result is printed.

If your data has no spaces at the end of the lines, use

$ sed '/^module/N;s/\n/ /' file
module x(a,b,c)
module y(d,e,f,
g,h,i)
module z(j,k,l)

Just in case you'd want this (assuming no spaces at end of input lines):

$ sed -e '/^module/bpp' -e 'H;$bpp' -e 'd' \
      -e ':pp' -e 'x;/^$/d;s/\n/ /g' file
module x(a,b,c)
module y(d,e,f, g,h,i)
module z(j,k,l)

Annotated sed script:

/^module/ b print_previous; # print previous record
H;                          # append this line to hold space
$         b print_previous; # print previous (last) record
d;                          # end processing this line

:print_previous;            # prints a record accumulated in the hold space
x;                          # swap in the hold space
/^$/ d;                     # if line is empty, delete it
s/\n/ /g;                   # replace embedded newlines by spaces
                            # (implicit print)
added 3 characters in body
Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k

What you want to do is to join the module lines with the next line.

Using sed:

$ sed '/^module/N;s/\n//' file
module  x(a,b,c)
module  y(d,e,f,
g,h,i)
module  z(j,k,l)

This is with your data copied and pasted as is, with spaces at the end of each line.

The sed command will print each line as it is read, but when it encounters a line that starts with the string module, it appends the next line with an embedded newline character in-between (this is what N does). We remove that newline character with a substitution before the result is printed.

If your data has no spaces at the end of the lines, use

$ sed '/^module/N;s/\n/ /' file
module x(a,b,c)
module y(d,e,f,
g,h,i)
module z(j,k,l)

Just in case you'd want this (assuming no spaces at end of input lines):

$ sed -e '/^module/bpp' -e '$bpp''H;$bpp' -e 'H;d''d' \
      -e ':pp' -e 'x;/^$/d;s/\n//g' file
module x(a,b,c)
module y(d,e,f, g,h,i)
module z(j,k,l)

Annotated sed script:

/^module/ b print_previous; # print previous record
$H;         b print_previous; # print previous (last) record
H;           # append this line to hold space
$         #b addprint_previous; this# lineprint toprevious hold(last) spacerecord
d;                          # end processing this line

:print_previous;            # prints a record accumulated in the hold space
x;                          # swap in the hold space
/^$/ d;                     # if line is empty, delete it
s/\n//g;                    # delete all embedded newlines
                            # (implicit print)

What you want to do is to join the module lines with the next line.

Using sed:

$ sed '/^module/N;s/\n//' file
module  x(a,b,c)
module  y(d,e,f,
g,h,i)
module  z(j,k,l)

This is with your data copied and pasted as is, with spaces at the end of each line.

The sed command will print each line as it is read, but when it encounters a line that starts with the string module, it appends the next line with an embedded newline character in-between (this is what N does). We remove that newline character with a substitution before the result is printed.

If your data has no spaces at the end of the lines, use

$ sed '/^module/N;s/\n/ /' file
module x(a,b,c)
module y(d,e,f,
g,h,i)
module z(j,k,l)

Just in case you'd want this (assuming no spaces at end of input lines):

$ sed -e '/^module/bpp' -e '$bpp' -e 'H;d' \
      -e ':pp' -e 'x;/^$/d;s/\n//g' file
module x(a,b,c)
module y(d,e,f, g,h,i)
module z(j,k,l)

Annotated sed script:

/^module/ b print_previous; # print previous record
$         b print_previous; # print previous (last) record
H;                          # add this line to hold space
d;                          # end processing this line

:print_previous;            # prints a record accumulated in the hold space
x;                          # swap in the hold space
/^$/ d;                     # if line is empty, delete it
s/\n//g;                    # delete all embedded newlines
                            # (implicit print)

What you want to do is to join the module lines with the next line.

Using sed:

$ sed '/^module/N;s/\n//' file
module  x(a,b,c)
module  y(d,e,f,
g,h,i)
module  z(j,k,l)

This is with your data copied and pasted as is, with spaces at the end of each line.

The sed command will print each line as it is read, but when it encounters a line that starts with the string module, it appends the next line with an embedded newline character in-between (this is what N does). We remove that newline character with a substitution before the result is printed.

If your data has no spaces at the end of the lines, use

$ sed '/^module/N;s/\n/ /' file
module x(a,b,c)
module y(d,e,f,
g,h,i)
module z(j,k,l)

Just in case you'd want this (assuming no spaces at end of input lines):

$ sed -e '/^module/bpp' -e 'H;$bpp' -e 'd' \
      -e ':pp' -e 'x;/^$/d;s/\n//g' file
module x(a,b,c)
module y(d,e,f, g,h,i)
module z(j,k,l)

Annotated sed script:

/^module/ b print_previous; # print previous record
H;                          # append this line to hold space
$         b print_previous; # print previous (last) record
d;                          # end processing this line

:print_previous;            # prints a record accumulated in the hold space
x;                          # swap in the hold space
/^$/ d;                     # if line is empty, delete it
s/\n//g;                    # delete all embedded newlines
                            # (implicit print)
added 606 characters in body
Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k
Loading
added 29 characters in body
Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k
Loading
added 199 characters in body
Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k
Loading
added 24 characters in body
Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k
Loading
Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k
Loading