How to merge different coulmn of differnt files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to merge different coulmn of differnt files
# 1  
Old 05-08-2008
How to merge different coulmn of differnt files

hello gurus ,


i want to merge different column from two different file. file struture is below.

file 1
-------



~information is given


Name class section
A 5 b
B 7 C
D 8 A


file 2
------


~information is given


Name grandtotal
A 20
B 30
D 15


in both the files data will come after "~information is given" and i want a result like

file3
-----
Name class section grandtotal

A 5 b 20
B 7 C 30
D 8 A 15


Any help is highly appericiated

Thanks
Rahul
# 2  
Old 05-08-2008
Seems to be more of a Home work que...

Guideline...

Cut the field and paste it into the other file...
i.e make use of cut ....paste filters...
# 3  
Old 05-08-2008
Granting that the two files have a common field in each line, I guess the join utility will solve your problem.


EDIT: join can make it but for your requirements it will need a lot of work than using cut and paste as suggested by shiv.

Last edited by yongitz; 05-08-2008 at 07:10 AM..
# 4  
Old 05-08-2008
Code:
$ cat file1
A 5 b
B 7 C
D 8 A

$ cat file2
A 20
B 30
D 15


join -1 1 -2 1 file1 file2
A 5 b 20
B 7 C 30
D 8 A 15

[ "-1 1" indicates join on 1st field of 1st file i.e file1 to " -2 1" 1st field of 2nd file i.e file2 ]

Last edited by Shivdatta; 05-08-2008 at 08:09 AM.. Reason: explaination added....
# 5  
Old 05-08-2008
Code:
awk 'NR==FNR{a[$1]=$2;next}$4=a[$1]' file2 file1 > file3

# 6  
Old 05-08-2008
I try to keep things simple. I am a novice user at best. Still lots to learn. But this this OS, any nix flavor, I try to keep the mentality "keep it simple stupid" and have one of those log blue log books that house command line, perl and ksh solutions that I have either figured out, read someplace or picked up from more advanced friends on forums like this ... it is gold to me and I keep it under lock and key ...

So with that in mind .. why not

pr -t -m file1 file2 > file3
# 7  
Old 05-08-2008
Quote:
Originally Posted by popeye
pr -t -m file1 file2 > file3
Did you check the output?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find arcsin for a coulmn containg values?

Hello everzone, I have a column of values and i need to find arcsin for evry value in this column. I am very new to shell scripting and would be glad to receive any help regarding this. Also i want them to be saved in the next column. Desired output: # # a arcsin(a) b arcsin(b) c ... (10 Replies)
Discussion started by: dinesh.n
10 Replies

2. Shell Programming and Scripting

Checking in a directory how many files are present and basing on that merge all the files

Hi, My requirement is,there is a directory location like: :camp/current/ In this location there can be different flat files that are generated in a single day with same header and the data will be different, differentiated by timestamp, so i need to verify how many files are generated... (10 Replies)
Discussion started by: srikanth_sagi
10 Replies

3. UNIX for Dummies Questions & Answers

rsync in osx - nondestructive with differnt names of root directory

normally I rsync -haPE source destination What I want to do is take a old ~ directory from an external drive and have it ONLY update missing files NOT replace existing files. excluding ~/library any help would be great. (3 Replies)
Discussion started by: briandanielz
3 Replies

4. Shell Programming and Scripting

how to add the lines of differnt files in a single file

My question is I am finding the new.txt file in the directory I am finding current.txt file in the directory in 2nd time I am finding fallback.txt in the directory in 3rd time all with find command now my question is let suppose I got all threee files in one directory then I want... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

5. Windows & DOS: Issues & Discussions

Windows mass copy files with same name in differnt folders

I have files existing with same names in the folders with date as display below c:\2010-09-10 <==== folder arr1.jpg arr2.jpg arr3.jpg arr4.jpg c:\2010-09-09 <==== folder arr1.jpg arr2.jpg c:\2010-09-08 <==== folder arr2.jpg arr3.jpg arr4.jpg ... (5 Replies)
Discussion started by: jville
5 Replies

6. Shell Programming and Scripting

Run a script in two differnt logins

Hi, I need to run a script in two different login's in the same server, but it is running only in one login, i have used the corresponding PATH for each login, but still it says " not authorized to put msg in queue". (2 Replies)
Discussion started by: savithavijay
2 Replies

7. Shell Programming and Scripting

replace value with double quotes of specific coulmn value in csv file

Hi, I am trying to replace a specific column values in a csv file with double quotes. Example: SNO,NAME,ZIPCODE,RANK 1,Robert,74538,12 2,Sam,07564,13 3,Kim, Ed,12345,14 Desired Output: SNO,NAME,ZIPCODE,RANK 1,Robert Ken,74538,12 2,Sam Mik,"07564",13 3,"Kim, Ed",12345,14 I... (3 Replies)
Discussion started by: techmoris
3 Replies

8. Shell Programming and Scripting

Merge files of differrent size with one field common in both files using awk

hi, i am facing a problem in merging two files using awk, the problem is as stated below, file1: A|B|C|D|E|F|G|H|I|1 M|N|O|P|Q|R|S|T|U|2 AA|BB|CC|DD|EE|FF|GG|HH|II|1 .... .... .... file2 : 1|Mn|op|qr (2 Replies)
Discussion started by: shashi1982
2 Replies

9. UNIX for Dummies Questions & Answers

ssh between two differnt versions

Hi, I am trying to ssh between two differnt versions on solaris boxes. The src box has the SSH Secure Shell 3.1.0 on sparc-sun-solaris2.6 and destination has SSH Version Sun_SSH_1.0, protocol versions 1.5/2.0. I am not trying for passwordless connection. I just want to get it working with a... (3 Replies)
Discussion started by: borncrazy
3 Replies

10. Shell Programming and Scripting

Paste coulmn wise

Hello, I need few clarification in types of paste command ussage in coulmn mode : In a directory i have few file distinguished as *.cpu *.vmem and *.mem and have coulmn entries like first cpu file 89576 89576 89576 89576 89576 89576 89576 second cpu file 46312 46312 46312 (3 Replies)
Discussion started by: er_aparna
3 Replies
Login or Register to Ask a Question