Generate files and use csv data to replace multiple variables in a template


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Generate files and use csv data to replace multiple variables in a template
# 1  
Old 09-06-2019
Generate files and use csv data to replace multiple variables in a template

I have a source csv file consists of first field as variable name, and the rest are site-specific information (converted from excel file, where site -specific values in columns). I am trying to create a file for every site using a template and replace the multiple variables with values from the csv. I used sed but it takes too many find/replace substitutions. What is the best way to use in my script? Thanks in advance.

Sample csv:
---------------
Code:
$ cat multiple-replace-Book1.csv 
var_Site,SITE-A1,SITE-A2,SITE-A3,SITE-B1,SITE-B2,SITE-C2,SITE-C3,SITE-C4
var_Name,HOST-A1,HOST-A2,HOST-A3,HOST-B1,HOST-B2,HOST-C2,HOST-C3,HOST-C4
var_Ipaddress,11.11.11.1,11.11.11.2,11.11.11.3,22.22.22.1,22.22.22.3,33.33.33.1,33.33.33.2,33.33.33.3
var_Port,22,22,22,22,22,22,22,22
var_Location,Bay1,Bay5,Bay3,Bay1,Bay2,Bay4,Bay2,Bay3
var_City,London,London,London,Rome,Rome,Paris,Paris,Paris
var_Comment,Comment1,Comment2,Comment3,Comment4,Comment5,Comment6,Comment7,Comment8
var_Data,Data1,Data2,Data3,Data4,Data5,Data6,Data7,Data8
var_String,String1,String2,String3,String4,String5,String6,String7,String8

Template content:
-----------------------
Code:
Test connection to $var_Host from $var_Location in $var_City
Use $var_Ipaddress $var_Port to connect
Note: Apply this $var_Data and $var_String
Check: $var_Comment

Desired output file = $var_Site-file.txt
-------------------------
Code:
SITE-A1-file.txt (with site-A1 values)
SITE-A2-file.txt (with site-A2 values)

Moderator's Comments:
Mod Comment
Code tags please as per forum rules

Last edited by Peasant; 09-06-2019 at 10:14 AM.. Reason: Added code tags.
# 2  
Old 09-06-2019
Where is the $var_Host info to be found?

--- Post updated at 17:39 ---

Assuming / inferring you mixed up var_Host and var_Name, how far would this:

Code:
awk -F, '
        {sub (/var_/, "", $1)
         MD = NF
         for (i=2; i<=NF; i++) DATA[$1, i] = $i
        }

END     {for (i=2; i<=MD; i++)  {print "Test connection to", DATA["Name", i], "from", DATA["Location", i], "in", DATA["City", i] ORS \
                                 "Use", DATA["Ipaddress", i], DATA["Port", i], "to connect" ORS \
                                 "Note: Apply this", DATA["Data", i], "and", DATA["String", i] ORS \
                                 "Check:", DATA["Comment", i]  > (DATA["Site", i] "-file.txt")
                                }
        }
 ' file

get you?


Be aware that your sampe file seems to be UTF-8 Unicode (with BOM) text - remove the BOM before applying this solution or adapt for the BOM.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 09-06-2019
Maybe you should reverse your initial matrix : ....


Code:
$cat my
var_Site,SITE-A1,SITE-A2,SITE-A3,SITE-B1,SITE-B2,SITE-C2,SITE-C3,SITE-C4
var_Name,HOST-A1,HOST-A2,HOST-A3,HOST-B1,HOST-B2,HOST-C2,HOST-C3,HOST-C4
var_Ipaddress,11.11.11.1,11.11.11.2,11.11.11.3,22.22.22.1,22.22.22.3,33.33.33.1,33.33.33.2,33.33.33.3
var_Port,22,22,22,22,22,22,22,22
var_Location,Bay1,Bay5,Bay3,Bay1,Bay2,Bay4,Bay2,Bay3
var_City,London,London,London,Rome,Rome,Paris,Paris,Paris
var_Comment,Comment1,Comment2,Comment3,Comment4,Comment5,Comment6,Comment7,Comment8
var_Data,Data1,Data2,Data3,Data4,Data5,Data6,Data7,Data8
var_String,String1,String2,String3,String4,String5,String6,String7,String8
$

might be easier to read for further processing if you take it that way :

Code:
$tr , "\n" <my | pr -t -9 -s" "
var_Site var_Name var_Ipaddress var_Port var_Location var_City var_Comment var_Data var_String
SITE-A1 HOST-A1 11.11.11.1 22 Bay1 London Comment1 Data1 String1
SITE-A2 HOST-A2 11.11.11.2 22 Bay5 London Comment2 Data2 String2
SITE-A3 HOST-A3 11.11.11.3 22 Bay3 London Comment3 Data3 String3
SITE-B1 HOST-B1 22.22.22.1 22 Bay1 Rome Comment4 Data4 String4
SITE-B2 HOST-B2 22.22.22.3 22 Bay2 Rome Comment5 Data5 String5
SITE-C2 HOST-C2 33.33.33.1 22 Bay4 Paris Comment6 Data6 String6
SITE-C3 HOST-C3 33.33.33.2 22 Bay2 Paris Comment7 Data7 String7
SITE-C4 HOST-C4 33.33.33.3 22 Bay3 Paris Comment8 Data8 String8
$

And then just

Code:
$tr , "\n" <my | pr -t -9 -s" " | (read a; while read a; do set -- $a ; echo "Test connection to $2 from $5 in $6
> Use $3 $4 to connect
> Note: Apply this $8 and $9
> Check: $7"; done )
Test connection to HOST-A1 from Bay1 in London
Use 11.11.11.1 22 to connect
Note: Apply this Data1 and String1
Check: Comment1
Test connection to HOST-A2 from Bay5 in London
Use 11.11.11.2 22 to connect
Note: Apply this Data2 and String2
Check: Comment2
Test connection to HOST-A3 from Bay3 in London
Use 11.11.11.3 22 to connect
Note: Apply this Data3 and String3
Check: Comment3
Test connection to HOST-B1 from Bay1 in Rome
Use 22.22.22.1 22 to connect
Note: Apply this Data4 and String4
Check: Comment4
Test connection to HOST-B2 from Bay2 in Rome
Use 22.22.22.3 22 to connect
Note: Apply this Data5 and String5
Check: Comment5
Test connection to HOST-C2 from Bay4 in Paris
Use 33.33.33.1 22 to connect
Note: Apply this Data6 and String6
Check: Comment6
Test connection to HOST-C3 from Bay2 in Paris
Use 33.33.33.2 22 to connect
Note: Apply this Data7 and String7
Check: Comment7
Test connection to HOST-C4 from Bay3 in Paris
Use 33.33.33.3 22 to connect
Note: Apply this Data8 and String8
Check: Comment8
 $


Of course you would have to adapt it to your need especially if the Comment contains some space, but i gave this shot just for the idea
The first "read a" is just to skip the header line.
These 2 Users Gave Thanks to ctsgnb For This Post:
# 4  
Old 09-12-2019
I was able to use the solution provided by RudiC for the required file output (yes, also after removing the BOM from source file).
I'm currently using the solution provided by ctsgnb on other projects where the data is transposed.

Thank you very much!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Export Oracle multiple tables to multiple csv files using UNIX shell scripting

Hello All, just wanted to export multiple tables from oracle sql using unix shell script to csv file and the below code is exporting only the first table. Can you please suggest why? or any better idea? export FILE="/abc/autom/file/geo_JOB.csv" Export= `sqlplus -s dev01/password@dEV3... (16 Replies)
Discussion started by: Hope
16 Replies

2. Shell Programming and Scripting

In PErl script: need to read the data one file and generate multiple files based on the data

We have the data looks like below in a log file. I want to generat files based on the string between two hash(#) symbol like below Source: #ext1#test1.tale2 drop #ext1#test11.tale21 drop #ext1#test123.tale21 drop #ext2#test1.tale21 drop #ext2#test12.tale21 drop #ext3#test11.tale21 drop... (5 Replies)
Discussion started by: Sanjeev G
5 Replies

3. Shell Programming and Scripting

Read multiple text files and copy data to csv

hi i need to extract lines from multiple files to a csv file. for example, i have these 3 files file1.txt date:29dec1980 caller:91245824255 called:8127766 file2.txt date:11apr2014 caller:9155584558 called:8115478 file3.txt date:25jun2015 caller:445225552 called:8117485 (30 Replies)
Discussion started by: lp.descamps
30 Replies

4. Shell Programming and Scripting

Extracting data from specific rows and columns from multiple csv files

I have a series of csv files in the following format eg file1 Experiment Name,XYZ_07/28/15, Specimen Name,Specimen_001, Tube Name, Control, Record Date,7/28/2015 14:50, $OP,XYZYZ, GUID,abc, Population,#Events,%Parent All Events,10500, P1,10071,95.9 Early Apoptosis,1113,11.1 Late... (6 Replies)
Discussion started by: pawannoel
6 Replies

5. UNIX for Dummies Questions & Answers

Need help combining txt files w/ multiple lines into csv single cell - also need data merge

:confused:Hello -- i just joined the forums. I am a complete noob -- only about 1 week into learning how to program anything... and starting with linux. I am working in Linux terminal. I have a folder with a bunch of txt files. Each file has several lines of html code. I want to combine... (2 Replies)
Discussion started by: jetsetter
2 Replies

6. Shell Programming and Scripting

Help with multiple variables into one row in CSV!

I'm new to shell scripting so I'm guessing I'm just not looking at this from the correct angle as this has to be a common task. What I'm trying to do is take data I've compiled for servers (Name, IPs, HBA WWN's, Storage, etc) and trying to turn that into one row in a CSV file. So File1:... (3 Replies)
Discussion started by: The_Grim_Coder
3 Replies

7. Shell Programming and Scripting

Find and replace variables using a csv table

I have a flat file (template) where I want to replace variables based upon a value in another file (csv). The variables in the template are named %VAR_X_z% The values are in the csv file and X is field 0 of each line and y field 1 and up. Example of the csv: Badidas, 13.00, 12.00, 11.00,... (8 Replies)
Discussion started by: biscayne
8 Replies

8. Shell Programming and Scripting

How to generate a csv files by separating the values from the input file based on position?

Hi All, I need help for doing the following. I have a input file like: aaaaaaaaaabbbbbbbbbbbbbbbbbbbb cccbbbbbaaaaaadddddaaaabbbbbbb now I am trying to generate a output csv file where i will have for e.g. 0-3 chars of each line as the first column in the csv, 4-10 chars of the line as... (3 Replies)
Discussion started by: babom
3 Replies

9. Shell Programming and Scripting

generate report based on data in files.

Hi All, I need to develop a shell script which does sanity check of a data file, as below. 1. For DATE columns, it should check if date is given in proper format or not? For example, if format of date is expected as DD-MON-YYYY HH24:MI:SS and we received the date in formation like DDMMYYYY HH24,... (1 Reply)
Discussion started by: ace_friends22
1 Replies

10. Shell Programming and Scripting

Using SED to generate new file from template

Hi there! I am using a BASH script to read a CSV file (containing variable values)using while read, and for every record I want SED to get a template from a file, and using the variables read from the CSV, write a new file. #!/bin/bash current_ifs=$IFS ; #backup original IFS, need ","... (12 Replies)
Discussion started by: ppucci
12 Replies
Login or Register to Ask a Question