Merge two lines using sed

Hi,

I am trying to merge two lines, first line starts with a particular pattern and second line ends with a particular pattern in a file.

Something like:

First line starts with say ABC
Second line ends with say XYZ

After a merge, the line should become ABC.......XYZ

I tried with

/usr/bin/sed '/^ABC/,/XYZ$/{
        s/\n//
 }'

But nothing happens.....Any ideas how to achieve this.

Does it really have to be sed, awk can do this quite easily:

awk '/ABC/ {F=1} F {P=P$0} !F; F&&/XYZ/ {print P;F=0}'

Thanks for the response. I am new to awk, the command is giving

awk: syntax error near line 1
awk: bailing out near line 1

Also, there are multiple instances in the file something like

ABCDEF
WXYZ
123
123
123
ABCDEF
WXYZ
 

I want end result to be

ABCDEFWXYZ
123
123
123
ABCDEFWXYZ

---------- Post updated at 12:11 PM ---------- Previous update was at 12:01 PM ----------

Found, i have to use /usr/xpg4/bin/awk

But the problem still persists since I have multiple instances of this pattern.

sed '/^ABC/{N;s/\n/ /;}' infile

Hi,

In my case, I have to look for two lines, one starting with a pattern and other ending with a pattern. So, I tried

sed '/^ABC/,/XYZ$/{N;s/\n/ /;}' infile
 

But the o/p is not as expected. The above command gives o/p as

ABCDEFWXYZ
123123
123ABCDEFWXYZ

but I want

ABCDEFWXYZ
123
123
123
ABCDEFWXYZ
123
123

Where do the two 123's at the end come from?

$ sed '/^ABC/,/XYZ$/{/^[^0-9]/N;s/\n//;}' file1
ABCDEFWXYZ
123
123
123
ABCDEFWXYZ

I guess, my bad here....using ABC 123 i was giving an example. The real scenario is I have an ldif file which has a DN value. I am doing an ldapsearch and o/p goes to a file. The problem is the whole DN value ends up in two different lines something like

dn: cn=......,ou=.......,dc=......,dc=
  co,dc=mk
objectClass:person
.
.
.
dn: cn=......,ou=.......,dc=......,dc=
  co,dc=mk
objectClass:inetorgperson

This is happening for most of the entries in the ldif starting from dn: and ending with dc=mk. I need to merge the above line so that the dn becomes complete

I tried using

/usr/bin/sed '/^dn:/,/dc=mk$/{ N
        s/\n//
 }'

But not with much luck. I want o/p to be

dn: cn=......,ou=.......,dc=......,dc=co,dc=mk
objectClass:person
.
.
.
dn: cn=......,ou=.......,dc=......,dc=co,dc=mk
objectClass:inetorgperson

Try it with some awk:

awk -F: 'NF>1{if(p)print p; p=$0}NF==1{p=p$0}END{print p}' infile
/usr/xpg4/bin/awk '/^dn:/ {S=1} S {gsub("^ *",""); L=L$0} /dc=mk/ {$0=L;S=L=""} !S'
awk '/^dn:/{s=$0;getline;$1=s$1}1' file

Original post implied the solution needs to join upto the dc=mk line, not just grab next line:

$ cat file
dn: cn=......,ou=.......,
   dc=......,
   dc=co,
   dc=mk
objectClass:person
.
.
.
dn: cn=......,
   ou=......,dc=......,
   dc=co,dc=mk
objectClass:inetorgperson
 
$ awk '/^dn:/{s=$0;getline;$1=s$1}1' file
dn: cn=......,ou=.......,dc=......,
   dc=co,
   dc=mk
objectClass:person
.
.
.
dn: cn=......,ou=......,dc=......,
   dc=co,dc=mk
objectClass:inetorgperson
$ awk '/^dn:/ {S=1} S {gsub("^ *",""); L=L$0} /dc=mk/ {$0=L;S=L=""} !S' file
dn: cn=......,ou=.......,dc=......,dc=co,dc=mk
objectClass:person
.
.
.
dn: cn=......,ou=......,dc=......,dc=co,dc=mk
objectClass:inetorgperson

Base on OP description

@danmero, but what happens with dn= records that are less then one line?

Base on OP my understanding is that all dn= record will always break in two lines. :confused: