Sponsored Content
Top Forums Shell Programming and Scripting Merge Multiple Files and Transpose Post 302436208 by alister on Friday 9th of July 2010 03:49:22 PM
Old 07-09-2010
Hello, mkastin:

f=file being processed
nunit=unit used for this component of nutritional data (i assume 'g' is for grams)
ndata=nutritional data

The order of the file arguments at the end of the command is critical.

Code:
awk '
  BEGIN {FS=OFS="^"}
  FNR==1 {++f}
  f==1 {nunit[$1]=$2}
  f==2 {ndata[$1]=ndata[$1] FS $3 FS nunit[$2]}
  f==3 {print FNR,$1,$2,$3 ndata[$1]}' \
NUTR_DEF.txt NUT_DATA.txt FOOD_DES.txt



Test run using your data:
Code:
$ awk '
>   BEGIN {FS=OFS="^"}
>   FNR==1 {++f}
>   f==1 {nunit[$1]=$2}
>   f==2 {ndata[$1]=ndata[$1] FS $3 FS nunit[$2]}
>   f==3 {print FNR,$1,$2,$3 ndata[$1]}' \
> NUTR_DEF.txt NUT_DATA.txt FOOD_DES.txt
1^~01001~^~0100~^~Butter, salted~^0.85^~g~^81.11^~g~^0.06^~g~^2.11^~g~
2^~01002~^~0100~^~Butter, whipped, with salt~^0.85^~g~^81.11^~g~^0.06^~g~^2.11^~g~
3^~01003~^~0100~^~Butter oil, anhydrous~^0.28^~g~^99.48^~g~^0.00^~g~^0.00^~g~
4^~01004~^~0100~^~Cheese, blue~^21.40^~g~^28.74^~g~^2.34^~g~^5.11^~g~
5^~01005~^~0100~^~Cheese, brick~^23.24^~g~^29.68^~g~^2.79^~g~^3.18^~g~
6^~01006~^~0100~^~Cheese, brie~^20.75^~g~^27.68^~g~^0.45^~g~^2.70^~g~
7^~01007~^~0100~^~Cheese, camembert~^19.80^~g~^24.26^~g~^0.46^~g~^3.68^~g~
8^~01008~^~0100~^~Cheese, caraway~^25.18^~g~^29.20^~g~^3.06^~g~^3.28^~g~
9^~01009~^~0100~^~Cheese, cheddar~^24.90^~g~^33.14^~g~^1.28^~g~^3.93^~g~
10^~01010~^~0100~^~Cheese, cheshire~^23.37^~g~^30.60^~g~^4.78^~g~^3.60^~g~

Regards,
Alister
This User Gave Thanks to alister For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Merge multiple .so files

Hi all, I am developing an application in Tcl, inwhich i have to load many modules written in C. I am converting those C modules into shared object(.so) files, and wrap it with my application using SWIG, for which i had the interface file. Now my question is, i have two different... (2 Replies)
Discussion started by: senthilvnr
2 Replies

2. Shell Programming and Scripting

Help to merge multiple .sql files

Hello all, I have a shell script that uses multiple .sql files. These .sql files mainly contain Oracle SQL queries to pull fields from the database. I want to place all the contents of these .sql files in one .sql file and have some parameter sent based on which the respective block or query... (6 Replies)
Discussion started by: snvniranjanrao
6 Replies

3. UNIX for Advanced & Expert Users

merge two column multiple files into one

Hi I have multiple files each with two columns and I need to combine all those file into a tab delimited file. (multiple entry with same name separated by a comma) The content of the files are as follows: --- file1.txt: name var1 aaa xx aaa gg bbb yy ddd zz --- file2.txt ... (8 Replies)
Discussion started by: mary271
8 Replies

4. UNIX for Dummies Questions & Answers

Merge multiple files

Hi All, How can I merge 3rd column of multiple files into 1 file, the column header in the merged file being the name of the file from which the 3rd column was taken. The first 2 columns of all the files are exactly same. Thanks for your help ! (3 Replies)
Discussion started by: newbie83
3 Replies

5. Shell Programming and Scripting

Merge column headers and transpose

Hello Everyone! I am new on this forum and this is my first post. I wish to apologize for my, not canonical, English. I would like to solve this problem but I have no clue of how do it!I will be grateful if someone could help me! I have a table like this: gene TF1 TF2 TF3 TF4 gene1 1 2 3 4... (5 Replies)
Discussion started by: giuliangiuseppe
5 Replies

6. UNIX for Dummies Questions & Answers

Merge columns from multiple files

Hi all, I've searched the web for a long time trying to figure out how to merge columns from multiple files. I know paste will append columns like so: paste file1 file2 file3 file4 file5 ... But this becomes inconvenient when you want to append a large number of files into a single file. ... (2 Replies)
Discussion started by: torchij
2 Replies

7. Shell Programming and Scripting

Merge the multiple text files into one file

Hi All, I am trying to merge all the text files into one file using below snippet cat /home/Temp/Test/Log/*.txt >> all.txt But it seems it is not working. I have multiple files like Output_ServerName1.txt, Output_ServreName2.txt I want to merge each file into one single file and... (6 Replies)
Discussion started by: sharsour
6 Replies

8. Shell Programming and Scripting

Merge columns from multiple files

Hello and Good day I have a lot of files with same number of rows and columns.$2 and $3 are the same in all files . I need to merge $2,$3,$6 from first file and $6 from another files. File1: $1 $2 $3 $4 $5 $6... (8 Replies)
Discussion started by: ali.seifaddini
8 Replies

9. Shell Programming and Scripting

Merge multiple files with common header

Hi all, Say i have multiple files x1 x2 x3 x4, all with common header (date, time, year, age),, How can I merge them to one singe file "X" in shell scripting Thanks for your suggestions. (2 Replies)
Discussion started by: msarguru
2 Replies

10. UNIX for Beginners Questions & Answers

Merge Multiple html files into one

Hi all I have written some code to write my output in html. As i have multiple servers, need to generate single html file. but my code is generating html file for each server. I have merged the files using below code. cat /home/*_FinalData.html > /home/MergedFinalData.html But how to... (1 Reply)
Discussion started by: Snehasish
1 Replies
GRI_MERGE(1)						      General Commands Manual						      GRI_MERGE(1)

NAME
gri_merge - merge multiple Gri output files into a single PostScript file. USAGE (style 1): gri_merge [OPTIONS] CxR a.ps b.ps ... > merged_file.ps Merges the files onto one page, in 'C' columns and 'R' rows. The CxR files are given in the order of words on a page. The page is pre- sumed to be 8.5x11in in size, as are all the input files, and the input files are sized to fit, and kept in natural scale. USAGE (style 2): gri_merge [OPTIONS] xcm ycm enlarge a.ps [b.ps ...] > merged_file.ps Where `enlarge' is a scale factor applied after offsetting `xcm' to the right and `ycm' upward. EXAMPLE (style 2): The following gri_merge 2 12 .5 a.ps 12 12 .5 b.ps 2 2 .5 c.ps 12 2 .5 d.ps > all.ps produces 4 panels from gri plots done using margins and sizes as specified in the following lines in a gri commandfile set x margin 2 set x size 15 set y margin 2 set y size 15 The OPTIONS, available if your 'perl' has 'getopts' library, are: -u graylevel -- set graylevel for underlay beneath panels, by default 0.75. Values range from 0 (black) to 1 (white), although a value of precisely 1 means do NOT draw underlay. -b graylevel -- Set value for background under individual panels, again 0 for black to 1 for white, with 1 meaning no drawing. -h -- Print this help message and quit. SEE ALSO
gri(1), gri_unpage(1) gri_merge 2009 GRI_MERGE(1)
All times are GMT -4. The time now is 01:40 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy