Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Generate files and use csv data to replace multiple variables in a template Post 303038564 by apalex on Friday 6th of September 2019 08:46:03 AM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
KNIFE-COOKBOOK-SITE(1)						    Chef Manual 					    KNIFE-COOKBOOK-SITE(1)

NAME
knife-cookbook-site - Install and update open source cookbooks SYNOPSIS
knife cookbook site sub-command (options) COOKBOOK SITE SUB-COMMANDS knife cookbook site provides the following subcommands: INSTALL
cookbook site install COOKBOOK [VERSION] (options) -D, --skip-dependencies Skip automatic installation of dependencies. -o, --cookbook-path PATH Install cookbooks to PATH -B, --branch BRANCH Default branch to work with [defaults to master] Uses git(1) version control in conjunction with the cookbook site to install community contributed cookbooks to your local cookbook reposi- tory. Running knife cookbook site install does the following: 1. A new "pristine copy" branch is created in git for tracking the upstream; 2. All existing cookbooks are removed from the branch; 3. The cookbook is downloaded from the cookbook site in tarball form; 4. The downloaded cookbook is untarred, and its contents commited via git; 5. The pristine copy branch is merged into the master branch. By installing cookbook with this process, you can locally modify the upstream cookbook in your master branch and let git maintain your changes as a separate patch. When an updated upstream version becomes available, you will be able to merge the upstream changes while main- taining your local modifications. Unless --skip-dependencies is specified, the process is applied recursively to all the cookbooks COOKBOOK depends on (via metadata depen- dencies). DOWNLOAD
knife cookbook site download COOKBOOK [VERSION] (options) -f, --file FILE The filename to write to --force Force download deprecated cookbook Downloads a specific cookbook from the Community site, optionally specifying a certain version. LIST
knife cookbook site list (options) -w, --with-uri Show corresponding URIs Lists available cookbooks from the Community site. SEARCH
knife cookbook site search QUERY (options) Searches for available cookbooks matching the specified query. SHARE
knife cookbook site share COOKBOOK CATEGORY (options) -k, --key KEY API Client Key -u, --user USER API Client Username -o, --cookbook-path PATH:PATH A colon-separated path to look for cookbooks in Uploads the specified cookbook using the given category to the Opscode cookbooks site. Requires a login user and certificate for the Opscode Cookbooks site. By default, knife will use the username and API key you've configured in your configuration file; otherwise you must explicitly set these values on the command line or use an alternate configuration file. UNSHARE
knife cookbook site unshare COOKBOOK Stops sharing the specified cookbook on the Opscode cookbooks site. SHOW
knife cookbook site show COOKBOOK [VERSION] (options) Shows information from the site about a particular cookbook. DESCRIPTION
The cookbook site, http://community.opscode.com/, is a cookbook distribution service operated by Opscode. This service provides users with a central location to publish cookbooks for sharing with other community members. knife cookbook site commands provide an interface to the cookbook site's HTTP API. For commands that read data from the API, no account is required. In order to upload cookbooks using the knife cookbook site share command, you must create an account on the cookbook site and configure your credentials via command line option or in your knife configuration file. EXAMPLES
Uploading cookbooks to the Opscode cookbooks site: knife cookbook site share example Other -k ~/.chef/USERNAME.pem -u USERNAME SEE ALSO
knife-cookbook(1) http://community.opscode.com/cookbooks AUTHOR
Chef was written by Adam Jacob adam@opscode.com with many contributions from the community. DOCUMENTATION
This manual page was written by Joshua Timberman joshua@opscode.com. Permission is granted to copy, distribute and / or modify this docu- ment under the terms of the Apache 2.0 License. CHEF
Knife is distributed with Chef. http://wiki.opscode.com/display/chef/Home Chef 10.12.0 June 2012 KNIFE-COOKBOOK-SITE(1)
All times are GMT -4. The time now is 05:49 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy