want to merge two consecutive lines.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers want to merge two consecutive lines.
# 1  
Old 04-25-2009
Question want to merge two consecutive lines.

Hi All,
I want to merge two consecutive lines.
Currently the output is :-->

crmplp1 cmis461 No Online
cmis462 No Offline
crmplp2 cmis462 No Online
cmis463 No Offline
crmplp3 cmis463 No Online
cmis461 No Offline


And I want to make it as -->
crmplp1 cmis461 No Online cmis462 No Offline
crmplp2 cmis462 No Online cmis463 No Offline


Can somebody suggest how to do it..?
# 2  
Old 04-25-2009
try this:

Code:
$ sed -n '/Online$/{
N
s/\n/ /g
p
d}' filename

cheers,
Devaraj Takhellambam

Last edited by Franklin52; 04-25-2009 at 10:14 AM.. Reason: adding code tags
# 3  
Old 04-25-2009
Or:

Code:
awk 'NR%2?ORS=FS:ORS=RS' file

# 4  
Old 04-25-2009
You could write the same command on a single line by adding the semicolons
Code:
sed -n '/Online$/{N;s/\n/ /g;p;d}' test

and because it is on a single line you can even remove the curly braces. In either case you do not need the global option 'g' on the substitution because the 'N' option only adds one line to the buffer so only one '\n' newline symbol will be found.
Code:
sed '/Online$/N;s/\n/ /p;d' test

# 5  
Old 04-25-2009
Hi.

The use of command paste seems straight-forward:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate paste for merging from STDIN.

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) paste
set -o nounset
echo

FILE=${1-data1}

echo " Data file $FILE:"
cat $FILE

echo
echo " Results:"
paste -d" " - - < $FILE

exit 0

Producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.11-x1, i686
Distribution        : Xandros Desktop 3.0.3 Business
GNU bash 2.05b.0
paste (coreutils) 5.2.1

 Data file data1:
crmplp1 cmis461 No Online
cmis462 No Offline
crmplp2 cmis462 No Online
cmis463 No Offline
crmplp3 cmis463 No Online
cmis461 No Offline

 Results:
crmplp1 cmis461 No Online cmis462 No Offline
crmplp2 cmis462 No Online cmis463 No Offline
crmplp3 cmis463 No Online cmis461 No Offline

See man page(s) for details ... cheers, drl
# 6  
Old 04-25-2009
Now its giving me output like this...

crmplp1 cmis461 No Online cmis462 No Offline
crmplp2 cmis462 No Online
cmis463 No Offline
crmplp3 cmis463 No Online cmis461 No Offline
crmplpp cmis464 No Online
cmis465 No Offline
crmplrp cmis465 No Online cmis464 No Offline


And now m not able to cut the columns using awk properly..

cmis461
cmis462
No
cmis463
cmis464
No
cmis465
# 7  
Old 04-25-2009
Question

I used thi -->

awk 'NR%2?ORS=FS:ORS=RS' file
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep three consecutive lines if each lines contains certain string

say we have : 2914 | REQUEST | whatever 2914 | RESPONSE | whatever 2914 | SUCCESS | whatever 2985 | RESPONSE | whatever 2986 | REQUEST | whatever 2990 | REQUEST | whatever 2985 | RESPONSE | whatever 2996 | REQUEST | whatever 2010 | SUCCESS | whatever 2013 | REQUEST | whatever 2013 |... (7 Replies)
Discussion started by: Saumitra Pandey
7 Replies

2. Shell Programming and Scripting

Removing 3 consecutive lines from script

Hi I need to remove three consecutive lines of code which appear multiple times during a script. Two of the lines also appear in other parts of the scripts and need to stay so I can't strip out the code per se - It needs to be the exact three lines. Hope that makes sense ! Any help much... (5 Replies)
Discussion started by: Grueben
5 Replies

3. UNIX for Dummies Questions & Answers

Eliminate consecutive lines with the same pattern

Hi, I would like to know how to remove lines which has the same pattern as the next line through sed/awk. Stream 39 (wan stream 7) Stream 40 (wan stream 8) WINQ Counter 115955 1 1613 (BYTE) 11204787 163 ... (2 Replies)
Discussion started by: sarn_nat
2 Replies

4. UNIX for Dummies Questions & Answers

Matching two patterns in the consecutive lines

Hi Experts I need to match 2 patterns consecutively and display 25 lines after that. 1st one - Error 2nd one - End string ( comes along with the pattern one) 3rd one - error Logic grep "ERROR OCCURRED :" trace.log | awk -v "ES=:" -v "SS=java.lang.NullPointerException" '{ if($NF ~... (8 Replies)
Discussion started by: senthil.ak
8 Replies

5. Shell Programming and Scripting

Grep couple of consecutive lines if each lines contains certain string

Hello, I want to extract from a file like : 20120530025502914 | REQUEST | whatever 20120530025502968 | RESPONSE | whatever 20120530025502985 | RESPONSE | whatever 20120530025502996 | REQUEST | whatever 20120530025503013 | REQUEST | whatever 20120530025503045 | RESPONSE | whatever I want... (14 Replies)
Discussion started by: black_fender
14 Replies

6. Shell Programming and Scripting

Removing consecutive lines in a file

We have very large transaction logs that have transactions which start with a line that starts with 'Begin :' and ends with a line that starts with 'End :'. For most transactions there is valid data between those two lines. I am trying to get rid of lines that look like this: Begin :... (11 Replies)
Discussion started by: deneuve01
11 Replies

7. Shell Programming and Scripting

Merge two non-consecutive lines based on line number or string

This is a variation of an earlier post found here: unixcom/shell-programming-scripting/159821-merge-two-non-consecutive-lines.html User Bartus11 was kind enough to solve that example. Previously, I needed help combining two lines that are non-consecutive in a file. Now I need to do the... (7 Replies)
Discussion started by: munkee
7 Replies

8. Shell Programming and Scripting

Merge two non-consecutive lines

Hello - First post here. I need help combining two lines that are non-consecutive in a file. Using sed, awk or perl preferably. So the file looks as follows. Please note, the "Line#:" is there only for reference. The lines can only be distinguished by whether they have "start" or "done" in... (2 Replies)
Discussion started by: munkee
2 Replies

9. UNIX Desktop Questions & Answers

How to concatenate consecutive lines

I have a few lines like -- feature 1, subfeat 0, type 3, subtype 1, value 0, -- feature 1, subfeat 0, type 1, subtype 1, value 0, I would like to concatenate the... (1 Reply)
Discussion started by: shivi707
1 Replies

10. Shell Programming and Scripting

Appending Consecutive lines

Hi, I have a file containing a single field on every row. What I need is to append one on to the end of another, e.g. The input file looks like this: nnnnn mmmmmm nnnnn mmmmmm I need it to look like this: nnnnn mmmmmm nnnnn mmmmmm Any ideas would be much appreciated,... (8 Replies)
Discussion started by: pondlife
8 Replies
Login or Register to Ask a Question