Shell scripting - need to arrange the columns from multiple file into a single file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell scripting - need to arrange the columns from multiple file into a single file
# 1  
Old 03-14-2013
Question Shell scripting - need to arrange the columns from multiple file into a single file

Hi friends

please help me on below,

i have 5 files like below
file1 is
Code:
x 10
y 20
z 15

file2 is
Code:
x 100
z  245

file3 is
Code:
y 78
z 23

file4 is
Code:
x 100
y 900
z 300

now i want the output as below( in a table format)
Code:
x 10    100    0    100
y 20    0       78   900
z 15    245    23   300


please help me it really urgent.. please help

Last edited by Franklin52; 03-14-2013 at 07:48 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 03-14-2013
I will let you figure out how to put a 0 when there is no data.

Code:
$ awk 'BEGIN{while (getline < "file1"){ map[$1]=$2 }}{x=map[$1];if ($1 in map){map[$1]=x" "$2}}END{for (i in map){print i,map[i]}}'  file2 file3 file4

Code:
x 10 100 100
y 20 78 900
z 15 245 23 300


cheers,
Devaraj Takhellambam

---------- Post updated at 09:45 AM ---------- Previous update was at 08:34 AM ----------

Try this....Hope this works.

Code:
awk 'BEGIN{while (getline < "file1"){ map[$1]=$2 }}{if (FNR == 1 && NR > 1){for (i in map){y=map[i];mn=split(y,m," ");if (mn < ln){map[i]=y " " 0;}}};x=map[$1];if ($1 in map){map[$1]=x " " $2;ln=split(map[$1],n," ");}}END{for (i in map){print i,map[i]}}' file2 file3 file4

O/P
Code:
x 10 100 0 100
y 20 0 78 900
z 15 245 23 300


cheers,
Devaraj Takhellambam
# 3  
Old 03-14-2013
@devtakh: if a one-liner is more than say 60 characters then it may be time to make it a multi-liner.
# 4  
Old 03-14-2013
or

Code:
$ awk 'FNR==1{++a}
        {A[$1,a]=$2;B[$1]++}
        END{for(i in B){
        for(j=1;j<=a;j++){
        t=A[i,j]?A[i,j]:"0"; s=s?s"\t"t:i"\t"t} print s; s="" }}
        ' file1 file2 file3 file4

x       10      100     0       100
y       20      0       78      900
z       15      245     23      300

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Output file name and file contents of multiple files to a single file

I am trying to consolidate multiple information files (<hostname>.Linux.nfslist) into one file so that I can import it into Excel. I can get the file contents with cat *Linux.nfslist >> nfslist.txt. I need each line prefaced with the hostname. I am unsure how to do this. --- Post updated at... (5 Replies)
Discussion started by: Kentlee65
5 Replies

2. Shell Programming and Scripting

Tabbed multiple csv files into one single excel file with using shell script not perl

Hi Experts, I am querying backup status results for multiple databases and getting each and every database result in one csv file. so i need to combine all csv files in one excel file with separate tabs. I am not familiar with perl script so i am using shell script. Could anyone please... (4 Replies)
Discussion started by: ramakrk2
4 Replies

3. Shell Programming and Scripting

Combining columns from multiple files into one single output file

Hi, I have 3 files with one column value as shown File: a.txt ------------ Data_a1 Data_a2 File2: b.txt ------------ Data_b1 Data_b2 Data_b3 Data_b4 File3: c.txt ------------ Data_c1 Data_c2 Data_c3 Data_c4 Data_c5 (6 Replies)
Discussion started by: vfrg
6 Replies

4. Shell Programming and Scripting

how to arrange datas in columns in shell script.

Hello All, I have datas in the below format. Mark 45 Steve 60 Johnson 79 Elizabeth 90 My requirement is to arrange the data in the order as shown in the attachment. My OS is Solaris. ... (6 Replies)
Discussion started by: vashece
6 Replies

5. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

6. Shell Programming and Scripting

How to arrange xml tags in single row using shell script?

I want to put one xml record in one row and so on... sample two records are here. <?xml version="1.0"?> <Object> <Header> <XCOMVers>V1.0</XCOMVers> <REPORT>XXXXX</REPORT> <CODE>002</CODE> </Header> <IssueCard> <Record> <L>CAR SYSTEM -SSSSS -</L> ... (3 Replies)
Discussion started by: sene_geet
3 Replies

7. Shell Programming and Scripting

How to arrange xml tags in single row using shell script?

I want to put one xml record in one row and so on... sample two records are here. <?xml version="1.0"?> <Object> <Header> <XCOMVers>V1.0</XCOMVers> <REPORT>XXXXX</REPORT> <CODE>002</CODE> </Header> <IssueCard> <Record> <L>CAR SYSTEM -SSSSS -</L> ... (1 Reply)
Discussion started by: sene_geet
1 Replies

8. Shell Programming and Scripting

Filtering issues with multiple columns in a single file

Hi, I am new to unix and would greatly appreciate some help. I have a file containing multiple colums containing different sets of data e.g. File 1: John Ireland 27_December_69 Mary England 13_March_55 Mike France 02_June_80 I am currently using the awk... (10 Replies)
Discussion started by: crunchie
10 Replies

9. Shell Programming and Scripting

how to arrange all lines in a file to a single line

Hi Gurus, I am a starter with shell script. Help me to achieve this. I have a file with lines given below. line1 line2 line3 . . etc How can I arrange the file which should look like line1,line2,line3,..,..,etc Any help on this is appreciated. (9 Replies)
Discussion started by: smv
9 Replies

10. Shell Programming and Scripting

Executing Multiple .SQL Files from Single Shell Script file

Hi, Please help me out. I have around 700 sql files to execute in a defined order, how can i do it from shell script (3 Replies)
Discussion started by: anushilrai
3 Replies
Login or Register to Ask a Question