Sponsored Content
Top Forums Shell Programming and Scripting How to combined to file with their values Post 302270437 by summer_cherry on Sunday 21st of December 2008 09:43:18 PM
Old 12-21-2008
Hi, below code can hlep you much i think.

it can combine any number of files with column of each file one by one as long as those files have same filed seperator.


Code:
#! /usr/bin/perl -w
sub combine{
	my $sep=shift;
	my $num=$#_+1;
	for ($i=0;$i<=$num-1;$i++){
		open FH,$_[$i] or die "Can not open file";
		while(<FH>){
			my @tmp=split($sep,$_);
			map {tr/\n//d} @tmp;
			for($j=0;$j<$#tmp;$j++){
				@{$hash{$tmp[0]}}->[$j*$num+$i]=$tmp[1+$j];
			}
		}
	}
	return \%hash;	
}
$ref=combine(" ","a.txt","b.txt","c.txt");
%hash=%{$ref};
for $key (keys %hash){
	print $key,":",join ":",@{$hash{$key}};
	print "\n";
}

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to combined file?

hello there unix programmer i have problem in combining file and their values.. here it is. in my file1 i have values 1010<tab>10<tab>11<tab>13 1011<tab>11<tab>12<tab>14 in my file2 i have values 1010<tab>22<tab>23<tab>24 1011<tab>23<tab>24<tab>25 my desired output in shell... (7 Replies)
Discussion started by: jantzen16
7 Replies

2. Shell Programming and Scripting

Merging of files with different headers to make combined headers file

Hi , I have a typical situation. I have 4 files and with different headers (number of headers is varible ). I need to make such a merged file which will have headers combined from all files (comman coluns should appear once only). For example - File 1 H1|H2|H3|H4 11|12|13|14 21|22|23|23... (1 Reply)
Discussion started by: marut_ashu
1 Replies

3. Shell Programming and Scripting

Replacing values in a file based on values in another file

Hi I have 2 files:- 1. List of files which consists of names of some output files. 2. A delimited file; delimted by "|" I want to replace the value of the $23 (23rd column) in the delimited file with name in the first file. It is always position to position. Meaning first row of the first... (5 Replies)
Discussion started by: pparthiv
5 Replies

4. Shell Programming and Scripting

remove values of a file one by one from 2nd file and then print the remaining values of 2nd file

Hi all, I have 2 files. One contains only 1 column and other one contains 2 columns, let say 1_col.txt and 2_col.txt respectively. Here, I will try to explain with an example. Input files : 1_col.txt 2_col.txt a a b x a c p ... (5 Replies)
Discussion started by: AshwaniSharma09
5 Replies

5. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

6. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

7. Shell Programming and Scripting

awk combined with an IF

Hi everybody! I try to printout a csv-file with the exeption of cell $1 and $4. what i tried so far: awk '{for(i = 1; i<=NF; i++);if(i == 1 || i == 4);else print($i)}' file.csv ..any ideas how it work and why my example fails? Thanks in advance! IMPe (3 Replies)
Discussion started by: IMPe
3 Replies

8. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

9. Shell Programming and Scripting

Combined sed+awk for lookup csv file

have written a combined sed+awk to perform a lookup operation which works but looking to enhance it. looking to match a record using any of the comma separated values + return selected fields from the record - including the field header. so: cat foo make,model,engine,trim,value... (6 Replies)
Discussion started by: jack.bauer
6 Replies

10. Shell Programming and Scripting

Taking key values from one file and extracting values from another file

Hi, I have two files with values in both. File1: cat 2 3 dog 4 5 elephant 6 7 camel 2 3 File2: ----+--gkf;ajf= ---+---- +----- cat -------=----+ 3 | 4 ----- dog ------++-- 5 | 9 ----++-- elephant | 5 | 7 ---++ camel ------ ++++_---- || 8 | 9 I want the final file as: cat 4... (1 Reply)
Discussion started by: npatwardhan
1 Replies
TV_CAT(1p)						User Contributed Perl Documentation						TV_CAT(1p)

NAME
tv_cat - Concatenate XMLTV listings files. SYNOPSIS
tv_cat [--help] [--output FILE] [FILE...] DESCRIPTION
Read one or more XMLTV files and write a file to standard ouput whose programmes are the concatenation of the programmes in the input files, and whose channels are the union of the channels in the input files. --output FILE write to FILE rather than standard output The treatment of programmes and channels is slightly different because for programmes, the ordering is important (typically programmes are processed or displayed in the same order as they appear in the input) whereas channels are just a set indexed by channel id. There is a warning if channel details clash for the same id. One more wrinkle is the credits (source, generator and so on), they are taken from one of the files and then there's a warning if the other files differ. If two input files have different character encodings, then it is not meaningful to combine their data (without recoding or other processing) and tv_cat die with an error message. This tool is rather useless, but it makes a good testbed for the XMLTV module. SEE ALSO
xmltv(5). AUTHOR
Ed Avis, ed@membled.com perl v5.14.2 2003-10-25 TV_CAT(1p)
All times are GMT -4. The time now is 08:25 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy