Combine multiple columns from multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combine multiple columns from multiple files
# 1  
Old 12-01-2008
Combine multiple columns from multiple files

Hi there,

I was wondering if someone can help me with this.

I am trying the combine multiple columns from multiple files into one file.

Example file 1:
Code:
c0t0d0 c0t2d0 # hostname vgname
c0t0d1 c0t2d1 # hostname vgname
c0t0d2 c0t2d2 # hostname vgname
c0t1d0 c0t3d0 # hostname vgname1
c0t1d3 c0t3d3 # hostname vgname1

Example file 2:
Code:
c0t0d0 c0t4d0 # hostname1 vgname
c0t0d1 c0t4d1 # hostname1 vgname
c0t0d2 c0t4d2 # hostname1 vgname
c0t1d0 c0t6d0 # hostname1 vgname1
c0t1d3 c0t6d3 # hostname1 vgname1

Preffered Output:
Code:
c0t2d0 c0t4d0 # hostname vgname
c0t2d1 c0t4d1 # hostname vgname
c0t2d2 c0t4d2 # hostname vgname
c0t3d0 c0t6d0 # hostname vgname1
c0t3d3 c0t6d3 # hostname vgname1

The only constant variable in this all is the vgname.
I've searched the forum simular posts, but when trying i don't get the desired output. The biggest problem is the formating of the output file.

Can someone help me with this.

Running HP-UX B.11.23 ia64
# 2  
Old 12-01-2008
I have just tried the join command. Not what i need right now.

I have also tried this one:

Code:
cat file1 | awk {'print $2'} > file3
cat file2 | awk {'print $1, $3, $4, $5'} > file4
paste file3 file4

There most be an easier way. Also with this option I have to check every ctd number in every line is are part of the same VG.

Last edited by martva; 12-01-2008 at 11:50 AM..
# 3  
Old 12-01-2008
Tools Patience...

Your followup message includes only: "Anyone? "

You did not provide the rules for how to combine the two files into one file.
It appears that you are taking the 2nd column of file one, making it the first column of your output; and taking the 2nd column to end of file two, making it columns two to end of your output.

Correct?
# 4  
Old 12-01-2008
Yes, that's correct.

So in the example above my output will be the following:

c0t2d0 c0t0d0 # hostname1 vgname
c0t2d1 c0t0d1 # hostname1 vgname
c0t2d2 c0t0d2 # hostname1 vgname
c0t3d0 c0t1d0 # hostname1 vgname1
c0t3d3 c0t1d3 # hostname1 vgname1
# 5  
Old 12-01-2008
Hammer & Screwdriver Here is an approach using awk

Code:
> cat file87
c0t0d0 c0t2d0 # hostname vgname
c0t0d1 c0t2d1 # hostname vgname
c0t0d2 c0t2d2 # hostname vgname
c0t1d0 c0t3d0 # hostname vgname1
c0t1d3 c0t3d3 # hostname vgname1
> cat file88
c0t0d0 c0t4d0 # hostname1 vgname
c0t0d1 c0t4d1 # hostname1 vgname
c0t0d2 c0t4d2 # hostname1 vgname
c0t1d0 c0t6d0 # hostname1 vgname1
c0t1d3 c0t6d3 # hostname1 vgname1

> cat f87_88
awk ' FILENAME=="file87" { dat[$1]=$2}
      FILENAME=="file88" { 
        if(FNR > 0)
        { print dat[$1]" "$2" "$3" "$4" "$5}
      
      } ' file87 file88
       
> f87_88
c0t2d0 c0t4d0 # hostname1 vgname
c0t2d1 c0t4d1 # hostname1 vgname
c0t2d2 c0t4d2 # hostname1 vgname
c0t3d0 c0t6d0 # hostname1 vgname1
c0t3d3 c0t6d3 # hostname1 vgname1

# 6  
Old 12-02-2008
He, it works.

Oke, say i want the files to be user provided, because not all files have the same name. Like this:

Code:
> f87_88 file87 file88

That means that the option FILENAME== is user provided.

Second, I need to be sure the devices in the file both correspond the same VG. So that if I run the script, I don't have to check the devices afterwards.

Regards

Edit:
My awk experience is very basic.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing carriage returns from multiple lines in multiple files of different number of columns

Hello Gurus, I have a multiple pipe separated files which have records going over multiple Lines. End of line separator is \n and records going over multiple lines have <CR> as separator. below is example from one file. 1|ABC DEF|100|10 2|PQ RS T|200|20 3| UVWXYZ|300|30 4| GHIJKL|400|40... (7 Replies)
Discussion started by: dJHa
7 Replies

2. Shell Programming and Scripting

Combine Multiple Files into Single One File One after other

I am trying to combine 4 .dat files into one single Output file Inputs are:- file123.dat, file256.dat, file378.dat & file490 Expected Output:- FileName=file1 {text from file1} EOF {blank line} FileName=file2 {text from file2} EOF {blank line} FileName=file3 {text from file3} EOF... (4 Replies)
Discussion started by: lancesunny
4 Replies

3. Shell Programming and Scripting

Merging multiple files from multiple columns

Hi guys, I have very basic linux experience so I need some help with a problem. I have 3 files from which I want to extract columns based on common fields between them. File1: --- rs74078040 NA 51288690 T G 461652 0.99223 0.53611 3 --- rs77209296 NA 51303525 T G 461843 0.98973 0.60837 3... (10 Replies)
Discussion started by: bartman2099
10 Replies

4. Shell Programming and Scripting

Compare multiple files with multiple number of columns

Hi, input file1 abcd 123 198 xyz1:0909090-0909091 ghij 234 999 xyz2:987654:987655 kilo 7890 7990 xyz3:12345-12357 prem 9 112 xyz5:97-1134 input file2 abcd 123 198 xyz1:0909090-0909091 -9.122 0 abed 88 98 xyz1:98989-090808 -1.234 1.345 ghij 234 999 xyz2:987654:987655 -10.87090909 5... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

5. UNIX for Dummies Questions & Answers

cutting multiple columns into multiple files

Hypothetically, suppose that file1 id v1 v2 v3 v4 v5 v6 v7..........v100 1 1 1 1 1 1 2 2 .....50 2 1 1 1 1 1 2 2 .....50 3 1 1 1 1 1 2 2 .....50 4 1 1 1 1 1 2 2 .....50 5 1 1 1 1 1 2 2 .....50 I want to write a loop such that I take the id# and the first 5 columns (v1-v5) into the... (3 Replies)
Discussion started by: johnkim0806
3 Replies

6. Shell Programming and Scripting

Combine columns from multiple files

Can anybody help on the script to combine/concatenate columns from multiple files input1 4 135 5 185 6 85 11 30 16 72 17 30 21 52 22 76 input2 2 50 4 50 6 33 8 62 10 25 12 46 14 42 15 46output (2 Replies)
Discussion started by: sdf
2 Replies

7. Emergency UNIX and Linux Support

Combine multiple Files into one big file

Hi Ppl, I have a requirement like i will be getting files of huge size daily and if the file size is so huge ,the files will be split into many parts and sent.The first file will have the header details followed by detail records and the consecutive files will have detail records and the last... (11 Replies)
Discussion started by: ganesh_248
11 Replies

8. Shell Programming and Scripting

combine multiple files by column into one files already sorted!

I have multiple files; each file contains a certain data in a column view simply i want to combine all those files into one file in columns example file1: a b c d file 2: 1 2 3 4 file 3: G (4 Replies)
Discussion started by: ahmedamro
4 Replies

9. UNIX for Dummies Questions & Answers

Combine multiple files with common string into one new file.

I need to compile a large amount of data with a common string from individual text files throughout many directories. An example data file is below. I want to search for the following string, "cc_sectors_1" and combine all the data from each file which contains this string, into one new... (2 Replies)
Discussion started by: GradStudent2010
2 Replies

10. Shell Programming and Scripting

need help with post:extract multiple columns from multiple files

hello, I will would be grateful if anyone can help me reply to my post extract multiple cloumns from multiple files; skip rows and include filenames; awk Please see this thread. Thanks manishabh (0 Replies)
Discussion started by: manishabh
0 Replies
Login or Register to Ask a Question