Help with merge Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with merge Shell Script
# 1  
Old 12-14-2010
Help with merge Shell Script

I have a file test.log
The content of the file is :
Code:
a:R_yz:1
a:R_cd:2
a:F_bc:0
a:F_xx:3
b:R_dg:5
b:R_gf:1
b:F_fd:4

I want the output is :
Code:
:a R_yz 1 3 3  
     :   R_cd 2
     :   F_bc 0
     :   F_xx 3

:b R_dg 5 6 4
     :    R_gf 1
     :    F_fd 4

NB: here 3 and 3 is R_yz + R_cd and F_bc + F_xx
6 and 4 is R_dg + R_gf and F_fd

Please help
Thanks in advance

Last edited by Scott; 12-14-2010 at 11:33 AM.. Reason: Code tags, please...
# 2  
Old 12-14-2010
Code:
awk -F: 'END {
  printf ":%s %s %s", p1, p2, v[1] 
  for (i = 1; i <= c; i += 2) 
    printf " %s", v[i] + v[i + 1]
  print r    
  }
NR > 1 && p1 != $1 {
  printf ":%s %s %s", p1, p2, v[1] 
  for (i = 1; i <= c; i += 2)
    printf " %s", v[i] + v[i + 1]
  print r; r = c = x; split(x, v)  
  }
{
  v[++c] = $3; p1 = $1; 
  c == 1 ? p2 = $2 : r = (r ? r : x) \
    RS "\t" FS OFS $2 OFS $3
  }' infile

# 3  
Old 12-14-2010
Lol Radulov ... did you have fun ? Smilie Smilie

@Scruti,
can you do shorter ? Smilie
# 4  
Old 12-14-2010
Quote:
Originally Posted by ctsgnb
Lol Radulov ... did you have fun ? Smilie Smilie

@Scruti,
can you do shorter ? Smilie
Smilie

The repeating code should be encapsulated in a function Smilie
# 5  
Old 12-14-2010
Here's one using Perl -

Code:
$
$
$ cat f46
a:R_yz:1
a:R_cd:2
a:F_bc:0
a:F_xx:3
b:R_dg:5
b:R_gf:1
b:F_fd:4
$
$
$
$ perl -lne '
  /(.*?):(.*?)_.*:(\d+)$/ and ($a,$b,$c) = ($1,$2,$3);
  if ($.==1) {$sum=$c; push @x,$_;
  } elsif ($a eq $preva) {
    push @x,$_;
    if ($b eq $prevb) {$sum+=$c}
    else {push @y,$sum; $sum=$c}
  } else {
    push @y,$sum;
    for ($i=0; $i<=$#x; $i++) {
      if ($i==0) {
        $x[$i] =~ s/:/ /g;
        print ":$x[$i]  @y";
      } else {
        $x[$i] =~ s/^.*?:/ /; $x[$i] =~ s/:/ /;
        print ": $x[$i]"
      }
    }
    @y = (); $sum=$c; @x=(); push @x,$_;
  }
  $preva = $a;
  $prevb = $b;
  END {push @y,$sum;
       for ($i=0; $i<=$#x; $i++) {
         if ($i==0) {
           $x[$i] =~ s/:/ /g;
           print ":$x[$i]  @y";
         } else {
           $x[$i] =~ s/^.*?:/ /; $x[$i] =~ s/:/ /;
           print ": $x[$i]"
         }
       }
  }
' f46
:a R_yz 1  3 3
:  R_cd 2
:  F_bc 0
:  F_xx 3
:b R_dg 5  6 4
:  R_gf 1
:  F_fd 4
$
$

tyler_durden
# 6  
Old 12-14-2010
@ctsgnb

OK, I'll have a go Smilie:
Code:
awk -F: '{$1=$1;f=":%s\n%s\n";if($1!=p){if(p)printf f,s OFS (n""?n:x),r RS; s=$0;r=x;p=$1}
          else r=r (r?RS:x)"\t: "$2" "$3;n+=$3}!(t=!t){s=s OFS n;n=x}
          END{printf f,s OFS (n""?n:x),r}' infile


Last edited by Scrutinizer; 12-14-2010 at 06:12 PM.. Reason: corrected for when n has only a single value and when it is 0 instead of ""
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 7  
Old 12-14-2010
Quote:
Originally Posted by Scrutinizer
OK, I'll have a go Smilie:
Code:
awk -F: '{$1=$1;f=":%s\n%s\n";if($1!=p){if(p)printf f,s,r RS; s=$0;r=x;p=$1}
          else r=r (r?RS:x)"\t: "$2" "$3;n+=$3}!(t=!t){s=s OFS n;n=x}
          END{printf f,s OFS (n?n:x),r}' file



hey Scrutinizer this may be asking extra work from you but ,
i usually read this forum to learn things, the above command will do the job but if we see its like greek and latin
so it will be nice if u had 2 line description also in ur solution what u doing
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Merge multi-lines into one single line using shell script or Linux command

Hi, Can anyone help me for merge the following multi-line log which beginning with a " and line ending with ": into one line. *****Original Log***** 087;2008-12-06;084403;"mc;;SYHLR6AP1D\LNZW;AD-703;1;12475;SYHLR6AP1B;1.1.1.1;0000000062;HGPDI:MSISDN=12345678,APNID=1,EQOSID=365;... (3 Replies)
Discussion started by: rajeshlinux2010
3 Replies

2. Shell Programming and Scripting

Shell script to merge and delete lines

POLY_STATS { EqName 103_tri Id 123 act_polyCount 1 act_polyValue 0 } POLY_STATS { EqName 103_tri Id 123 pass_polyCount 2 pass_polyValue 0 } POLY_STATS { EqName 103 Id 123 mes_polyCount 2 mes_polyValue 0 (5 Replies)
Discussion started by: Jag02
5 Replies

3. Shell Programming and Scripting

Bash/shell merge similar lines

Hello, I've been working on a bash script to parse through firewall logs (cisco). I'm nearing the end and have a dilemma. My data looks as such (actual data is several gigs worth of logs - without the headers): sourceIP destinationIP destinationProtocol destinationPort 1.1.1.1 2.2.2.2 ... (2 Replies)
Discussion started by: umang2382
2 Replies

4. Shell Programming and Scripting

Merge two files on awk/shell

Hi, i have two files like these: FILE 1 00:0f:35:1b:0c:00 1402691094.750049000 00:0f:35:1b:0c:00 1402691087.474893000 44:d3:ca:fd:a2:08 1402691091.865127000 30:e4:db:c1:df:de 1402691090.192464000 FILE 2_ 00:0F:35 Cisco Systems, Inc 30:E4:DB Cisco Systems, Inc I need a file 3, that... (5 Replies)
Discussion started by: bertiko
5 Replies

5. Shell Programming and Scripting

How to merge Expect script inside shell script?

Hi I have two scripts one is Expect and other is shell. I want to merge Expect code in to Shell script so that i can run it using only one script. Can somebody help me out ? Order to execute: Run Expect_install.sh first and then when installation completes run runTests.sh shell script. ... (1 Reply)
Discussion started by: ashish_neekhra
1 Replies

6. Shell Programming and Scripting

Shell code for split/merge the file with certain number of columns

i have a file with some number of colums and each row have different number of fields. now my target is supposed to be, each row should have same number of columns. example: src file: 111,S3mobile,Samsu ng 112,Lu mia,Nok ia 113,brav ia,Sonyerichson tgt file: 111,S3mobile,Samsung... (8 Replies)
Discussion started by: abhilash_nakka
8 Replies

7. Shell Programming and Scripting

How to merge some files with diffrent sizes into one excel file using shell?

Hii I have these files , and I want to merge them in an excel file each file have two columns file1 title1 1 1 2 2 3 3 file2 title2 5 5 6 6 7 7 8 8 9 9 (10 Replies)
Discussion started by: maryre89
10 Replies

8. UNIX for Dummies Questions & Answers

Need script to merge two spreadsheets

Hello all, I am very new to scripting and I have a challenging problem I would like to write a code for. Essentially, I would like to merge two tab-delimited spreadsheets that have identical column and row labels, one contains numbers, the other contains labels (text and numbers): Spreadsheet 1... (19 Replies)
Discussion started by: torchij
19 Replies

9. Shell Programming and Scripting

Shell Scripting: Compare pattern in two files and merge the o/p in one.

one.txt ONS.1287677000.820.log 20Oct2010 ONS.1287677000.123.log 21Oct2010 ONS.1287677000.456.log 22Oct2010 two.txt ONS.1287677000.820.log:V AC CC EN ONS.1287677000.123.log:V AC CC EN ONS.1287677000.820.log:V AC CC EN In file two.txt i have to look for pattern which column one... (17 Replies)
Discussion started by: saluja.deepak
17 Replies

10. Shell Programming and Scripting

shell script to merge files

Can anybody help me out with this problem " a shell program that takes one or any number of file names as input; sorts the lines of each file in ascending order and displays the non blank lines of each sorted file and merge them as one combined sorted file. The program generates an error... (1 Reply)
Discussion started by: arya
1 Replies
Login or Register to Ask a Question