Sponsored Content
Top Forums Shell Programming and Scripting Merge records based on multiple columns Post 302897927 by summer_cherry on Thursday 17th of April 2014 02:48:02 AM
Old 04-17-2014
python

Code:
import re
hash={}
with open("a.txt") as file:
 for line in file:
  arr=re.findall('"[^"]+"',line.replace("\n",""))
  key=" ".join(arr[0:13])
  if key not in hash:
    hash[key]=[]
  hash[key].append(arr[14:])   
  
for i in hash:
    print(i,end=" ")
    print('"'," ".join(j[1].replace('"',"") for j in sorted(hash[i],key=lambda x: int(x[0].replace('"',"")))),'"',sep="")

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merge text files while combining the multiple header/trailer records into one each.

Situation: Our system currently executes a job (COBOL Program) that generates an interface file to be sent to one of our vendors. Because this system processes information for over 100,000 employees/retirees (and growing), we'd like to multi-thread the job into processing-groups in order to... (4 Replies)
Discussion started by: oordonez
4 Replies

2. Shell Programming and Scripting

Multiple records based on :

Hi , I have the below source source data 1|2|3|:123:abc|4 1|2|a| | 5 1|2|3|4|:a:s:D.....:n|t Target data should be 1|2|3|:123:abc|4 1|2|3|:123:abc|4 1|2|a| | 5 1|2|3|4|:a:s:D.....:n|t 1|2|3|4|:a:s:D.....:n|t 1|2|3|4|:a:s:D.....:n|t 1|2|3|4|:a:s:D.....:n|t (3 Replies)
Discussion started by: mora
3 Replies

3. Shell Programming and Scripting

file merge based on common columns

I have two files 1.txt 34, ABC, 7, 8, 0.9 35, CDE, 6.5, -2, 0.01 2.txt 34, ABC, 9, 6, -1.9 35, CDE, 8.5, -2.3, 5.01 So in both files common columns are 1 and 2 so final o/p should look like 34, ABC, 7, 8, 0.9, 9, 6, -1.9 35, CDE, 6.5, -2, 0.01, 8.5, -2.3, 5.01 I tried using... (3 Replies)
Discussion started by: manas_ranjan
3 Replies

4. Shell Programming and Scripting

count the unique records based on certain columns

Hi everyone, I have a file result.txt with records as following and another file mirna.txt with a list of miRNAs e.g. miR22, miR123, miR13 etc. Gene Transcript miRNA Gar Nm_111233 miR22 Gar Nm_123440 miR22 Gar Nm_129939 miR22 Hel Nm_233900 miR13 Hel ... (6 Replies)
Discussion started by: miclow
6 Replies

5. UNIX for Dummies Questions & Answers

How do I merge multiple columns into one column?

Hi all, I'm looking for a way to merge multiple columns (from one file) into a single column in an output file. The file I have looks somewhat like this: @HWI-ST212 1:N:0 AGTCCTACCGGGAGT + @@@DDDDDHHHHHII @HWI-ST212 1:N:0 CGTTTAAAAATTTCT + @;@B;DDDDH?:F;F... (4 Replies)
Discussion started by: Vnguyen
4 Replies

6. UNIX for Dummies Questions & Answers

Merge columns from multiple files

Hi all, I've searched the web for a long time trying to figure out how to merge columns from multiple files. I know paste will append columns like so: paste file1 file2 file3 file4 file5 ... But this becomes inconvenient when you want to append a large number of files into a single file. ... (2 Replies)
Discussion started by: torchij
2 Replies

7. Shell Programming and Scripting

Merge columns from multiple files

Hello and Good day I have a lot of files with same number of rows and columns.$2 and $3 are the same in all files . I need to merge $2,$3,$6 from first file and $6 from another files. File1: $1 $2 $3 $4 $5 $6... (8 Replies)
Discussion started by: ali.seifaddini
8 Replies

8. Shell Programming and Scripting

Merge files based on columns

011111123444 1234 1 20000 011111123444 1235 1 30000 011111123446 1234 3 40000 011111123447 1234 4 50000 011111123448 1234 3 50000 File2: 011111123444,Rsttponrfgtrgtrkrfrgtrgrer 011111123446,Rsttponrfgtrgtr 011111123447,Rsttponrfgtrguii 011111123448,Rsttponrfgtrgtjiiu I have 2 files... (4 Replies)
Discussion started by: vinus
4 Replies

9. UNIX for Dummies Questions & Answers

Merge selective columns from files based on common key

Hi, I am trying to selectively merge two files based on keys reported in the 1st column. File1: #file1-header1 file1-header2 111 qwe rtz uio 198 asd fgh jkl 165 yxc 789 poi uzt rew 89 lkj File2: #file2-header2 file2-header2 165 ghz nko2 ... (2 Replies)
Discussion started by: dovah
2 Replies

10. UNIX for Beginners Questions & Answers

Merge multiple columns into one using cat

I will like to merge several files using 'cat', but I observe the output is not consistent. the merge begins at the last line of the first file. file1.txt: 1234 1234 1234 file2.txt: aaaa bbbb cccc dddd cat file1.txt file2.txt > file3.txt file3.txt: 1234 1234 1234aaaa bbbb cccc... (13 Replies)
Discussion started by: geomarine
13 Replies
MouseX::NativeTraits::HashRef(3pm)			User Contributed Perl Documentation			MouseX::NativeTraits::HashRef(3pm)

NAME
MouseX::NativeTraits::HashRef - Helper trait for HashRef attributes SYNOPSIS
package Stuff; use Mouse; has 'options' => ( traits => ['Hash'], is => 'ro', isa => 'HashRef[Str]', default => sub { {} }, handles => { set_option => 'set', get_option => 'get', has_no_options => 'is_empty', num_options => 'count', delete_option => 'delete', pairs => 'kv', }, ); DESCRIPTION
This module provides a Hash attribute which provides a number of hash-like operations. PROVIDED METHODS
These methods are implemented in MouseX::NativeTraits::MethodProvider::HashRef. get($key, $key2, $key3...) Returns values from the hash. In list context return a list of values in the hash for the given keys. In scalar context returns the value for the last key specified. set($key => $value, $key2 => $value2...) Sets the elements in the hash to the given values. delete($key, $key2, $key3...) Removes the elements with the given keys. exists($key) Returns true if the given key is present in the hash. defined($key) Returns true if the value of a given key is defined. keys Returns the list of keys in the hash. sorted_keys Returns the list of sorted keys in the hash. values Returns the list of values in the hash. kv Returns the key/value pairs in the hash as an array of array references. for my $pair ( $object->options->pairs ) { print "$pair->[0] = $pair->[1] "; } elements Returns the key/value pairs in the hash as a flattened list. clear Resets the hash to an empty value, like "%hash = ()". count Returns the number of elements in the hash. Also useful for not empty: "has_options => 'count'". is_empty If the hash is populated, returns false. Otherwise, returns true. accessor If passed one argument, returns the value of the specified key. If passed two arguments, sets the value of the specified key. METHODS
meta method_provider_class helper_type SEE ALSO
MouseX::NativeTraits perl v5.14.2 2011-12-04 MouseX::NativeTraits::HashRef(3pm)
All times are GMT -4. The time now is 06:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy