Merge two lines using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merge two lines using sed
# 1  
Old 12-16-2010
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
Code:
/usr/bin/sed '/^ABC/,/XYZ$/{
        s/\n//
 }'

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

Last edited by Scott; 12-17-2010 at 07:37 AM.. Reason: Please use code tags
# 2  
Old 12-17-2010
Does it really have to be sed, awk can do this quite easily:
Code:
awk '/ABC/ {F=1} F {P=P$0} !F; F&&/XYZ/ {print P;F=0}'

# 3  
Old 12-17-2010
Thanks for the response. I am new to awk, the command is giving
Code:
awk: syntax error near line 1
awk: bailing out near line 1

Also, there are multiple instances in the file something like
Code:
ABCDEF
WXYZ
123
123
123
ABCDEF
WXYZ

I want end result to be
Code:
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.

Last edited by Scott; 12-17-2010 at 07:38 AM.. Reason: Code tags
# 4  
Old 12-17-2010
Code:
sed '/^ABC/{N;s/\n/ /;}' infile

# 5  
Old 12-17-2010
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
Code:
sed '/^ABC/,/XYZ$/{N;s/\n/ /;}' infile

But the o/p is not as expected. The above command gives o/p as
Code:
ABCDEFWXYZ
123123
123ABCDEFWXYZ

but I want
Code:
ABCDEFWXYZ
123
123
123
ABCDEFWXYZ
123
123


Last edited by Scott; 12-17-2010 at 07:38 AM.. Reason: Code tags
# 6  
Old 12-17-2010
Where do the two 123's at the end come from?
Code:
$ sed '/^ABC/,/XYZ$/{/^[^0-9]/N;s/\n//;}' file1
ABCDEFWXYZ
123
123
123
ABCDEFWXYZ

# 7  
Old 12-19-2010
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
Code:
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
Code:
/usr/bin/sed '/^dn:/,/dc=mk$/{ N
        s/\n//
 }'

But not with much luck. I want o/p to be
Code:
dn: cn=......,ou=.......,dc=......,dc=co,dc=mk
objectClass:person
.
.
.
dn: cn=......,ou=.......,dc=......,dc=co,dc=mk
objectClass:inetorgperson


Last edited by Scott; 12-19-2010 at 10:19 AM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed: how to merge two lines moving matched pattern to end of previous line

hello everyone, im new here, and also programming with awk, sed and grep commands on linux. In my text i have many lines with this config: 1 1 4 3 1 1 2 5 2 2 1 1 1 3 1 2 1 3 1 1 1 2 2 2 5 2 4 1 3 2 1 1 4 1 2 1 1 1 3 2 1 1 5 4 1 3 1 1... (3 Replies)
Discussion started by: satir
3 Replies

2. Shell Programming and Scripting

Merge Lines

Hello I have an input as below this is test we are( ) one end of description I am looking for output this is test we are () one end of description (2 Replies)
Discussion started by: Tomlight
2 Replies

3. Shell Programming and Scripting

Merge lines

Hello I have a file with CAR 23 COLOR 12 CAR 44 COLOR 12 CAR 55 COLOR 20 SIZE BIG CAR 56 CAR 57 COLOR 11 How can merge the CAR and the COLOR + SIZE (if there are COLOR,SIZE) CAR 23 COLOR 12 CAR 44 COLOR 12 CAR 55 COLOR 20 SIZE BIG CAR 56 CAR 57 COLOR 11 Every line begin in... (4 Replies)
Discussion started by: sharong
4 Replies

4. Shell Programming and Scripting

Sed - merge lines bw 2 specific characters

Hi, I have a bash script and I am looking for a command that will merge specific lines together. Sample Data: registration time = 1300890272 Id = 1 setd = 0 tagunt = 26 tagId=6, length=8, value= tagId=9, length=5, value= tagId=7, length=2, value= tagId=16, length=2, value= tagId=32,... (8 Replies)
Discussion started by: Winsarc
8 Replies

5. Shell Programming and Scripting

remove blank lines and merge lines in shell

Hi, I'm not a expert in shell programming, so i've come here to take help from u gurus. I'm trying to tailor a csv file that i got to make it work for the LOAD FROM command. I've a datatable csv of the below format - --in file format xx,xx,xx ,xx , , , , ,,xx, xxxx,, ,, xxx,... (11 Replies)
Discussion started by: dvah
11 Replies

6. UNIX for Dummies Questions & Answers

Merge lines together in unix

I have a file like this. Pls help me to solve this in ksh (I should look for only Message : 111 and need to print the start time to end time Need to ignore other type of messages. Ex: if first message is 111 and second message is 000 or anything else then ignore the 2nd one and print start time... (7 Replies)
Discussion started by: mnjx
7 Replies

7. Shell Programming and Scripting

merge lines

Hi guys in input every 1st line 1st ID value located in 2nd line 1st occurrence . I need to print them down accordingly.. Thanx in advance input rs1040480_XXXXX.value rs1040481_YYYYY.value rs1040482_TXXXX.value 0.7408157 0.3410044 0.7408157 ... (7 Replies)
Discussion started by: stateperl
7 Replies

8. Shell Programming and Scripting

Merge two lines

Hi I have two lines of data formatted as displayed below shop, price, remark, date a,#N/A,order to -fd, 20091011 and would like it to be shop:a price:#N/A remark:order to -fd date:20091011 How can I do it? Many thanks (2 Replies)
Discussion started by: lalelle
2 Replies

9. Shell Programming and Scripting

Merge lines into one

Source data file from oracle, terminated by ",". 'Cause some of fields have \r\n, then those lines were splitted into multiple lines in the expoted data file. Just vi this file, and found ^M. How to concatenate these line into one if it has a ^M at then end. thanks, (7 Replies)
Discussion started by: anypager
7 Replies

10. Shell Programming and Scripting

Use sed to merge multiple lines

Hi all: I have a file in which the contents are as following: ... This is a test ONE TWO Hello, world! XXX YYY CCC test again three, four five six seven world AAA BBB QQQ test eight, nine world (3 Replies)
Discussion started by: xb88
3 Replies
Login or Register to Ask a Question