Visit Our UNIX and Linux User Community


Writing out 2nd column into one file from multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Writing out 2nd column into one file from multiple files
# 1  
Old 10-23-2009
Hammer & Screwdriver Writing out 2nd column into one file from multiple files

I have several files that are being generated every 20 minutes. Each file contains 2 columns. The 1st column is Text, 2nd column is Data.

I would like to generate one single file from all these files as follows:

One instance of 1st column Text, followed by 2nd column Data separated by Comma.

All files are of the same line length and have 2 columns of data.

example:
File #1 USER bob
WAIT 20
JOBS 2
DONE 9

File #2
USER john
WAIT 30
JOBS 4
DONE 3


File #3
USER lisa
WAIT 120
JOBS 7
DONE 4

Combined Single File:

USER bob, john, lisa
WAIT 20, 30, 120
JOBS 2, 4, 7
DONE 9, 3, 4

Thanks appreciate any help
-- Subha
# 2  
Old 10-23-2009
Code:
awk '{A[$1] = A[$1] " " $2} END { for( a in A ) print a, A[a] }' file[123]

WAIT  20 30 120
JOBS  2 4 7
USER  bob john lisa
DONE  9 3 4

# 3  
Old 10-23-2009
Thank you.. however for example in my Data.. I have several instances of say USER or WAIT on many lines and want data on those lines to be unique.

USER bob
WAIT 20
RUN 3
DONE 3

USER maria
WAIT 10
RUN 4
DONE 6

So after 20 minutes the same user name bob, maria would appear with different values for WAIT RUN and DONE..

Can u suggest how do i preserve each line as it is..

thank you
# 4  
Old 10-23-2009
It helps - really helps - if you say what you want all up front, and not by installment!

I'll look into it.

---------- Post updated at 01:51 AM ---------- Previous update was at 01:47 AM ----------

I've looked into it, and I'm not sure I understand what you want!

OK OK I think I got it.
# 5  
Old 10-23-2009
Sorry about the confusion.. here is the details. My script generates data every 20 minutes.

File #1
USER maria
RUN 5
WAIT 6
DONE 7
QUEUE short
TOTAL 10

USER bob
RUN 7
WAIT 3
DONE 2
QUEUE long
TOTAL 8

After 20 minutes
File #2
USER maria
RUN 2
WAIT 4
DONE 3
QUEUE short
TOTAL 12

USER bob
RUN 4
WAIT 5
DONE 5
QUEUE long
TOTAL 13

At the end of 2 hours the concatenated file would look like this

USER maria, maria, maria, maria, maria, maria
RUN 5, 2, 5, 6, 7, 9
WAIT 6, 2, 4, 6, 7, 8
DONE 7, 2, 4, 5, 7, 8
QUEUE short, short, short, short, short, short
TOTAL 10, 12, 13, 14, 15, 16

USER bob, bob, bob, bob, bob, bob
RUN 7, 4, 5, 6, 8, 9, 10
WAIT 3, 4, 6, 8, 2, 4
DONE 2, 3, 4, 2, 4, 2
QUEUE long, long, long, long, long, long
TOTAL 8, 10, 12, 13, 14, 16

Hope this illustration makes it clearer..

thank you
# 6  
Old 10-23-2009
OK, to keep it simple (and so you can easily maintain it ;-) ) based on the two input files you gave.

Code:
awk '
  /^USER/ { USER[$2] = USER[$2] " " $2; U = $2 }
  /^RUN/  { RUN[U] = RUN[U] " " $2 }
  /^WAIT/  { WAIT[U] = WAIT[U]" " $2 }
  /^DONE/  { DONE[U] = DONE [U] " " $2 }
  /^QUEUE/  { QUEUE[U] = QUEUE[U] " " $2 }
  /^TOTAL/  { TOTAL[U] = TOTAL[U] " " $2 }

  END { for ( u in USER ) {
            print "USER " USER[u]
            print "RUN " RUN[u]
            print "WAIT " WAIT[u]
            print "DONE " DONE[u]
            print "QUEUE " QUEUE[u]
            print "TOTAL " TOTAL[u]
            print ""
            }}

' file[12]


USER  bob bob
RUN  7 4
WAIT  3 5
DONE  2 5
QUEUE  long long
TOTAL  8 13

USER  maria maria
RUN  5 2
WAIT  6 4
DONE  7 3
QUEUE  short short
TOTAL  10 12


Last edited by Scott; 10-23-2009 at 09:26 PM.. Reason: why do all my uppercase U's look like lowercase u's in the top bit??

Previous Thread | Next Thread
Test Your Knowledge in Computers #26
Difficulty: Easy
IPv6 addresses are 256 bits in length.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to copy a column of multiple files and paste into new excel file (next to column)?

I have data of an excel files as given below, file1 org1_1 1 1 2.5 100 org1_2 1 2 5.5 98 org1_3 1 3 7.2 88 file2 org2_1 1 1 2.5 100 org2_2 1 2 5.5 56 org2_3 1 3 7.2 70 I have multiple excel files as above shown. I have to copy column 1, column 4 and paste into a new excel file as... (26 Replies)
Discussion started by: dineshkumarsrk
26 Replies

2. Shell Programming and Scripting

Locate the files in the first column and copy the files in 2nd column

#cat data.txt file1 folder1 file2 thisforfile2 file3 thisfolderforfile3 lata4 folder4 step 1: create the folder first in column 2 for i in `awk '{print $2}' data.txt` do mkdir /home/data/$i done step 2: locate the files in column1 and stored them into a file for i in... (17 Replies)
Discussion started by: kenshinhimura
17 Replies

3. Shell Programming and Scripting

Join 2nd column of multiple files

Dear All, I have many files formatted like this: file1.txt: 1/2-SBSRNA4 18 A1BG 3 A1BG-AS1 6 A1CF 0 A2LD1 1 A2M 1160 file2.txt 1/2-SBSRNA4 53 A1BG 1 A1BG-AS1 7 A1CF 0 A2LD1 3 A2M 2780 (5 Replies)
Discussion started by: paolo.kunder
5 Replies

4. Shell Programming and Scripting

Difference between 2 files, one with 1 column and 2nd file with multiple columns

Hi, I need to find the difference between 2 files in unix and write the result in the new file File1: A B File2: X 123 hajkd Y 345 adjfka A 123 djafjhd B 678 dsndjks Output file: X 123 hajkd Y 345 adjfka Thanks. (6 Replies)
Discussion started by: nani1984
6 Replies

5. Shell Programming and Scripting

Combining multiple column files into one with file name as first row

Hello All, I have several column files like this $cat a_b_s1.xls 1wert 2tg 3asd 4asdf 5asdf $cat c_d_s2.xls 1wert 2tg 3asd 4asdf 5asdf desired put put $cat combined.txt s1 s2 (2 Replies)
Discussion started by: avatar_007
2 Replies

6. Shell Programming and Scripting

Split a file into multiple files based on line numbers and first column value

Hi All I have one query,say i have a requirement like the below code should be move to diffent files whose maximum lines can be of 10 lines.Say in the below example,it consist of 14 lines. This should be moved logically using the data in the fisrt coloumn to file1 and file 2.The data of first... (2 Replies)
Discussion started by: sarav.shan
2 Replies

7. UNIX for Dummies Questions & Answers

Writing a loop to merge multiple files by common column

I have 100 data files labelled 250.1.txt through 250.100.txt. The second column of the data files partially match (there is about %90 overlap). Each data file has 4 columns. I want the merge all these text files by the matching values in the second column. In the output, the first column should... (1 Reply)
Discussion started by: evelibertine
1 Replies

8. Shell Programming and Scripting

comparing column of two different files and print the column from in order of 2nd file

Hi friends, My file is like: Second file is : I need to print the rows present in file one, but in order present in second file....I used while read gh;do awk ' $1=="' $gh'" {print >> FILENAME"output"} ' cat listoffirstfile done < secondfile but the output I am... (14 Replies)
Discussion started by: CAch
14 Replies

9. UNIX for Dummies Questions & Answers

Comparing the 2nd column in two different files and printing corresponding 9th columns in new file

Dear Gurus, I am very new to UNIX. I appreciate your help to manage my files. I have 16 files with equal number of columns in it. Each file has 9 columns separated by space. I need to compare the values in the second column of first file and obtain the corresponding value in the 9th column... (12 Replies)
Discussion started by: Unilearn
12 Replies

10. Shell Programming and Scripting

Split single file into multiple files based on the number in the column

Dear All, I would like to split a file of the following format into multiple files based on the number in the 6th column (numbers 1, 2, 3...): ATOM 1 N GLY A 1 -3.198 27.537 -5.958 1.00 0.00 N ATOM 2 CA GLY A 1 -2.199 28.399 -6.617 1.00 0.00 ... (3 Replies)
Discussion started by: tomasl
3 Replies

Featured Tech Videos