Help with merge Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with merge Shell Script
# 8  
Old 12-14-2010
Yes it is probably too cryptic, which is because it was a bit of a challenge to see if it could be done shorter, all in good fun Smilie but quite a bit less readable then Radoulov's version..

Anyway. I hope this clarifies things a bit:

-F:Use : as a record separator
$1=$1Assign a value to one of the fields and thus recalculate the record, replacing FS with OFS, i.e. : with a space
f=":%s\n%s\n"Set the format to a colon, followed by string-newline-string-newline
if($1!=p)p holds the first filed of the previous record. If it is no the same then a new group of lines have a arrived.
{if(p)printf f,s OFS (n""?n:x),r RSif the variable p contains a value then print the string s concatenated to n (if it is is not empty, the "" force it to be interpreted as string) and then string r, using the formatting contained in the variable f followed by a newline character (RS)
s=$0;r=x;p=$1}Set the the string s to the first line ($0) of the group of lines.
elseif $1 is the same as the previous line:
r=r (r?RS:x)"\t: "$2" "$3append a tab a colon and a space followed by field 2 and 3 with a space in between. Precede all this by a newline if r already contains characters and by nothing if r is empty (x is unset and thus is equal to "")...
n+=$3add $3 to variable n
!(t=!t){s=s OFS n;n=x} if negating t becomes !t and the result is 1 (which effectively means: every other line), then add the number n at the end of string s.
END{printf f,s OFS (n""?n:x),r}After all lines are processed print s followed by n (if it is is not empty, the "" force it to be interpreted as string) and then string r, using formatting contained in variable f

Last edited by Scrutinizer; 12-14-2010 at 06:16 PM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 9  
Old 12-15-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 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


Thanks a lot,
it's work fine
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