Sponsored Content
Full Discussion: Need help on merging
Top Forums Shell Programming and Scripting Need help on merging Post 302929869 by Scrutinizer on Sunday 28th of December 2014 01:42:54 AM
Old 12-28-2014
Sure:

Code:
awk '
  BEGIN {
    n=4                      # set number of buckets
    m=1                      # initialize emptiest bucket
  }

  FNR==1 {                   # if a new file is starting to be read (FNR is line number per file)
    if(NR>1) {               # if it is not the very first file (NR is total line number for all files)
      A[m]+=sz               # add the size of the previous file to the emptiest bucket
      f="outfile" m          # specify the bucket output file name
      printf "%s",s>f        # print to last file from memory to emptiest bucket file
      m=1                    # set emptiest bucket to 1
      for(i=2; i<=n; i++)    # for every the other bucket
        if(A[m]>A[i])        # if the minimum bucket is fuller then that bucket
          m=i                # make that bucket the new minimum
      s=x                    # clear the file memory
      sz=0                   # clear its size
    }
  }

  {
    s=s $0 ORS               # Add next line of file to memory
    sz+=length               # Add the number of characters on that line to the size
  }

  END {
    if(NR>1)printf "%s",s>f  # print the last file in memory to the emptiest bucket file
  }
' $(ls -drS file*)           # read the files in reverse sorted size order

Hope this helps
This User Gave Thanks to Scrutinizer For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merging Help

Hi Gurus, I need a help in merging the files. I have nearly 7 files and the files will have time stamp in it. I need to merger these files condition is it is not necessary that all the 7 files has to be there. suppose if i have only 3 files availabe out of these 7 then i need to merge... (3 Replies)
Discussion started by: kumarc
3 Replies

2. Shell Programming and Scripting

Merging arrays

Hi all, I need some help in merging arrays. I have two arrays and using korn shell Array1 AB23 AB24 Array2 CD00 CD01 CD02 Elements from array 1 should always alternate with elements of arrays 2 i.e the result should look like AB23CD00 AB24CD01 AB23CD02 Any help is appreciated.... (4 Replies)
Discussion started by: jakSun8
4 Replies

3. Shell Programming and Scripting

merging files

Thanks in advance I have 2 files having key field in each.I would like to join both on common key.I have used join but not sucessful. The files are attached here . what i Want in the output is on the key field SLS OFFR . I have used join commd but not successful. File one ======= SNO ... (6 Replies)
Discussion started by: vakharia Mahesh
6 Replies

4. UNIX for Dummies Questions & Answers

Merging files

Hi i have two files say file 1 contents are A B C D E I have file2 contents are B E F G C K I want to have new file like A B (4 Replies)
Discussion started by: ssuresh1999
4 Replies

5. Shell Programming and Scripting

merging two files

Friends, os: redhat enterprise linux/SCO UNIX5.0 I have two files and I would like to merge on given key value. Now I have tried with join commd but it does not supporte multiple delimiters. and if records length is not fixed. join -a1 5 -a2 1 -t -o file1 file2 > outname Can any... (7 Replies)
Discussion started by: vakharia Mahesh
7 Replies

6. Shell Programming and Scripting

merging

Hi all, I have 2 files. I want to merge a portion or column in file 2 into file 1. file 1 - not tab or space delimited B_1 gihgjfhdj| hgfkddlldjljldjlddl B_2 gihgjddshjgfhs| hgfkddlldjljldjlddl B_3 gihgjfhdj| hgfkddlldjljldjlddlhgjdhdhjdhjhdjhdjhgdj file2 -... (7 Replies)
Discussion started by: Lucky Ali
7 Replies

7. Shell Programming and Scripting

Merging two files with merging line by line

Hi, I have two files and i want to merge it like, file1.txt --------- abc cde efg file2.txt ------- 111 222 333 Output file should be, -------------- abc 111 (2 Replies)
Discussion started by: rbalaj16
2 Replies

8. Shell Programming and Scripting

Merging

Hi, I have searched the forums for a solution but I haven't found a perfect answer, and I'm a bit of a novice, so I hope someone can help: I have 2 files: file1: Chr1 139311 1/1:37,3,0:19 Chr1 139350 1/1:67,6,0:19 Chr1 139404 1/1:0,0,0:7 Chr1 152655 0/1:0,0,0:3 Chr1 152718... (2 Replies)
Discussion started by: ljk
2 Replies

9. Shell Programming and Scripting

Merging 2 Arrays

I am trying to create a script that combines 2 arrays: #!/bin/bash read -a unix #(a c e g) read -a test #(b d f) #now I want to merge ${unix} with ${test}, one after another such that the result would be: (abcdefg) #I've tried quite a few options and can't seem to make it work (5 Replies)
Discussion started by: pbmitch
5 Replies

10. Shell Programming and Scripting

Interval merging

I do have a file (file1) with a specified interval of 500 counts file1 0 500 500 1000 1000 1500 1500 2000 2000 2500 2500 3000 3000 3500 3500 4000 4000 4500 4500 5000 5000 5500 5500 6000 6000 6500 6500 7000 7000 7500 7500 8000 (3 Replies)
Discussion started by: Kanja
3 Replies
libtar_hash_new(3)						  C Library Calls						libtar_hash_new(3)

NAME
libtar_hash_new, libtar_hash_free, libtar_hash_next, libtar_hash_prev, libtar_hash_getkey, libtar_hash_search, libtar_hash_add, lib- tar_hash_del - hash table routines SYNOPSIS
#include <libtar.h> libtar_hash_t *libtar_hash_new(int num, int (*hashfunc)()); void libtar_hash_free(libtar_hash_t *h, void (*freefunc)()); int libtar_hash_next(libtar_hash_t *h, libtar_hashptr_t *hp); int libtar_hash_prev(libtar_hash_t *h, libtar_hashptr_t *hp); int libtar_hash_search(libtar_hash_t *h, libtar_hashptr_t *hp, void *data, int (*matchfunc)()); int libtar_hash_getkey(libtar_hash_t *h, libtar_hashptr_t *hp, void *data, int (*matchfunc)()); int libtar_hash_add(libtar_hash_t *h, void *data); int libtar_hash_del(libtar_hash_t *h, libtar_hashptr_t *hp); DESCRIPTION
The libtar_hash_new() function creates a new hash with num buckets and using hash function pointed to by hashfunc. If hashfunc is NULL, a default hash function designed for 7-bit ASCII strings is used. The libtar_hash_free() function deallocates all memory associated with the hash structure h. If freefunc is not NULL, it is called to free memory associated with each node in the hash. The libtar_hash_next() and libtar_hash_prev() functions are used to iterate through the hash. The libtar_hashptr_t structure has two fields: bucket, which indicates the current bucket in the hash, and node, which is a pointer to the current node in the current bucket. To start at the beginning or end of the hash, the caller should initialize hp.bucket to -1 and hp.node to NULL. The libtar_hash_search() function searches iteratively through the hash h until it finds a node whose contents match data using the match- ing function matchfunc. Searching begins at the location pointed to by hp. The libtar_hash_getkey() function uses the hash function associated with h to determine which bucket data should be in, and searches only that bucket for a matching node using matchfunc. Searching begins at the location pointed to by hp. The libtar_hash_add() function adds data into hash h. The libtar_hash_del() function removes the node referenced by hp. RETURN VALUE
The libtar_hash_new() function returns a pointer to the new hash structure, or NULL on error. The libtar_hash_next() and libtar_hash_prev() functions return 1 when valid data is returned, and 0 at the end of the hash. The libtar_hash_getkey() and libtar_hash_search() functions return 1 when a match is found, or 0 otherwise. The libtar_hash_add() function returns 0 on success, or -1 on error (and sets errno). The libtar_hash_del() function returns 0 on success, or -1 on error (and sets errno). SEE ALSO
libtar_list_new(3) University of Illinois Jan 2000 libtar_hash_new(3)
All times are GMT -4. The time now is 04:56 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy