Joining multi-line output to a single line in a group


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Joining multi-line output to a single line in a group
# 1  
Old 03-07-2012
MySQL Joining multi-line output to a single line in a group

Hi,

My Oracle query is returing below o/p

Code:
---------------------------------------------------------- 
Ins    trnas             value 
a      lkp1               x 
a      lkp1               y 
b      lkp1               a 
b      lkp2               x 
b      lkp2               y 
------------------------------------ 
now I want to convert this o/p  to  below o/p using perl script
 
ins trans    value 
a   lkp1       xy 
b   lkp1       a 
b   lkp2       xy


Thanks
gvk25
# 2  
Old 03-07-2012
Code:
perl -lane 'if($.==1){print;next}; $x{"$F[0] $F[1]"} .= $F[2]; END{for (sort keys %x){print "$_ $x{$_}"}}' inputfile

# 3  
Old 03-07-2012
I am storing my o/p of the query in an array,I want to use an array instead of inputfile.
Dow we have any other way to implement above logic

Code:
@ins=(a,a,b,b,b); @trans=(lkp1,lkp1,lkp1,lkp1,lkp2,lkp2) @value(x,y,a,x,y)


Last edited by gvk25; 03-07-2012 at 04:53 AM..
# 4  
Old 03-07-2012
Code:
#! /usr/bin/perl -w
use strict;

my @ins   = qw ( a    a    b    b    b    );
my @trans = qw ( lkp1 lkp1 lkp1 lkp2 lkp2 );
my @value = qw ( x    y    a    x    y    );
my %x;
my $prev = "";

for (0..$#ins) {
    if ($prev eq "$ins[$_] $trans[$_]") {
        print "$value[$_]";
    }
    else {
        print "\n$ins[$_] $trans[$_] $value[$_]";
        $prev = "$ins[$_] $trans[$_]";
    }    
}

This User Gave Thanks to balajesuri For This Post:
# 5  
Old 03-07-2012
thanks very much for you help

---------- Post updated at 03:16 PM ---------- Previous update was at 03:08 PM ----------

I want to call a funtion with the value of each group how can I acheive this


I mean after concatenation of particular group values

Code:
i.e  group 1    a  lkp1   xy 
                    sub test(xy)
     group 2    b  lkp2   a  
                    sub test(xy)
     group 3    b lkp2  xy
                     sub test (xy)
 
and so ....
 
a lkp1 xy
b lkp1 a
b lkp2 xy

# 6  
Old 03-07-2012
Code:
awk 'NR>1{x=$1 FS $2;n=NR}!b[x]++{b[n]=x}{a[x]=((x in a)?a[x]:z)$NF}END{for(i=0;++i<=n;) if(i in b) print b[i],a[b[i]]}' yourfile

# 7  
Old 03-07-2012
Can you explain me the code what you are doing

---------- Post updated at 03:22 PM ---------- Previous update was at 03:20 PM ----------

hi Balaji can you explain the logic which you have implemented.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multi line log files to single line format

I want to read the log file which was generate from other command . And the output was having multi line in log files for job name and server name. But i need to make all the logs on one line Source file 07/15/2018 17:02:00 TRANSLOG_1700 Server0005_SQL ... (2 Replies)
Discussion started by: ranjancom2000
2 Replies

2. Shell Programming and Scripting

Help with reformat single-line multi-fasta into multi-line multi-fasta

Input File: >Seq1 ASDADAFASFASFADGSDGFSDFSDFSDFSDFSDFSDFSDFSDFSDFSDFSD >Seq2 SDASDAQEQWEQeqAdfaasd >Seq3 ASDSALGHIUDFJANCAGPATHLACJHPAUTYNJKG ...... Desired Output File >Seq1 ASDADAFASF ASFADGSDGF SDFSDFSDFS DFSDFSDFSD FSDFSDFSDF SD >Seq2 (4 Replies)
Discussion started by: patrick87
4 Replies

3. UNIX for Dummies Questions & Answers

Remove multi line and single line comments

Hi, I am trying to remove multi line and single line comments like examples below I have tried this pattern. it works fine for single line comments and multi line comments in a single line only. but this fails when the comments are extended in multiple lines as shown in the comment 2 of... (3 Replies)
Discussion started by: ahmedwaseem2000
3 Replies

4. Shell Programming and Scripting

joining multi-line file into single lines

Hi, I have a file like mentioned below..For each specific id starting with > I want to join the sequence in multiple lines to a single line..Is there a simple way in awk or sed to do this >ENST00000558922 cdna:KNOWN TCCAGGATCCAGCCTCCCGATCACCGCGCTAGTCCTCGCCCTGCCTGGGCTTCCCCAGAG... (2 Replies)
Discussion started by: Diya123
2 Replies

5. Shell Programming and Scripting

Merge multi-line output into a single line

Hello I did do a search and the past threads doesn't really solve my issue. (using various awk commands) I need to combine the output from java -version into 1 line, but I am having difficulties. When you exec java -version, you get: java version "1.5.0_06" Java(TM) 2 Runtime... (5 Replies)
Discussion started by: flagman5
5 Replies

6. Shell Programming and Scripting

How to use Perl to join multi-line into single line

Hello, Did anyone know how to write a perl script to merge the multi-line into a single line where each line with start at timestamp Input--> timestamp=2009-11-10-04.55.20.829347; a; b; c; timestamp=2009-11-10-04.55.20.829347; aa; bb; cc; (5 Replies)
Discussion started by: happyday
5 Replies

7. Shell Programming and Scripting

How to use Perl to merge multi-line into single line

Hi, Can anyone know how to use perl to merge the following multi-line information which beginning with "BAM" into one line. For each line need to delete the return and add a space. Please see the red color line. ******Org. Multi-line) BAM admin 101.203.57.22 ... (3 Replies)
Discussion started by: happyday
3 Replies

8. Shell Programming and Scripting

awk help required to group output and print a part of group line and original line

Hi, Need awk help to group and print lines to format the output as shown below INPUT FORMAT set echo on set heading on set spool on /* SCHEMA1 */ CREATE TABLE T1; /* SCHEMA1 */ CREATE TABLE T2; /* SCHEMA1 */ CREATE TABLE T3; /* SCHEMA1 */ CREATE TABLE T4; /* SCHEMA1 */ CREATE TABLE T5;... (5 Replies)
Discussion started by: rajan_san
5 Replies

9. Shell Programming and Scripting

Multi-line output to single line

Hello, How can I take the following output: outputa outputb outputc and turn it into single line ouput, with a single space between each field like below: outputa outputb outputc (7 Replies)
Discussion started by: LinuxRacr
7 Replies

10. UNIX for Dummies Questions & Answers

Joining lines to single line in VI

Dear friends, In VI, I have these data shown below: Line1 Line2 Line3 Line4 How can I JOIN these line to the first line? When I finished I should have: Line1 Line2 Line3 Line4 is there a text length limit of how long a single line can be in VI? Thank you much! (10 Replies)
Discussion started by: bobo
10 Replies
Login or Register to Ask a Question