Help with multiple variables into one row in CSV!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with multiple variables into one row in CSV!
# 1  
Old 01-16-2013
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:
Code:
ServerA

File2:
Code:
192.1.1.1
192.2.2..2

File3:
Code:
600.00GB

What I want is:
Code:
ServerA, 192.1.1.1, 192.2.2.2, 600GB

I have a script that will run some commands against my storage array, perform some simple awk functions to filter out the data as listed above. I don't have to append the data to seperate files but I figured that would keep it clean. I'd like to run a for loop against a list of servers taht will pull all this data in one server at a time and then append it as one row per server in a single "master" file so I can import it into excel for reporting.

Thanks for your help!

Last edited by joeyg; 01-16-2013 at 01:50 PM.. Reason: Please wrap commands and data with CodeTags
# 2  
Old 01-16-2013
Not sure I understand

A cat command could put the three files into one, and then a tr command could change the carriage-returns to commas.

Off the top of my head, something like
Code:
cat file1 file2 file3 | tr '\n' ',' >file4

But that assumes your 3 files only contain that little bit of data.
This User Gave Thanks to joeyg For This Post:
# 3  
Old 01-16-2013
Code:
cat file1 file2 file3 | awk '{ $1=$1 }1' RS= OFS=,

# 4  
Old 01-16-2013
Joey you are spot on my friend. Were I more knowledgabkle with the code I'm sure i could explain this better because I know from working at large companies that its fairly common to use commands, snmpgets, etc to pull raw data then format that to a csv or something similar and then import it into a database for others to access via websites or reporting tools.

I've gotten to the point I can pull this data in via scripts, for loops or whatever but getting it into a format that a database or a spreadsheet app likes afterwards has been the tricky part for me.

So to better explain what I'm doing here is the output of a command that tells me most the info I need about a server, its HBAs and the storage it has:

Code:
 
Symmetrix ID      : 00019010111
Host Name         : ServerA
Identifiers Found : 100000001111111
                         100000002222222
 
Device  Cap(MB)  Attr  Dir:P
------  -------  ----  ----
0155          3         7A:0,10A:0
0156          3         7A:0,10A:0
0157          3         7A:0,10A:0
-----------------------------
MB Total: 9

I'm using some simple awk commands to format this data as I listed in the first post. I don't have to use three different files, one would suffice and your cat/tr command combo against that one file would make it one row that I can then redirect to a master list.

So step one of my process would be to:

1. Run the command above in a loop against a list of a few hundred servers.
2. Pipe this data to a temp file per server as the loop is executed
3. Run your cat/tr command against the temp file and >> to my master list
4. Repeat until I have each server in my master list!

This works perfectly for me but being I'm not very knowledgable on the subject I would love to here yours or anyones opinion on how best to do this as I'm sure this is done often by server admins, network guys, etc for various admin reporting.

Thanks again!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

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... (3 Replies)
Discussion started by: apalex
3 Replies

2. Shell Programming and Scripting

Row Count in .csv file

Hi, I have to find the count of rows starting with "E," in given a.csv file . Sample Data File. E,2333AED,A,MC3,25,31-MAY-18 E,2333AED,A,MC3,25,31-MAY-18 CYMC3 25AED 0000 E,2333CZK,A,MC3,25,31-MAY-18 CYMC3 25CZK 0000 E,2333EUR,A,MC3,25,31-MAY-18... (3 Replies)
Discussion started by: Prabhakar Y
3 Replies

3. Shell Programming and Scripting

Row bind multiple csv files having different column headers

All, I guess by this time someone asked this kind of question, but sorry I am unable to find after a deep search. Here is my request I have many files out of which 2 sample files provided below. File-1 (with A,B as column headers) A,B 1,2 File-2 (with C, D as column headers) C,D 4,5 I... (7 Replies)
Discussion started by: ks_reddy
7 Replies

4. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

5. Shell Programming and Scripting

CSV to SQL insert: Awk for strings with multiple lines in csv

Hi Fellows, I have been struggling to fix an issue in csv records to compose sql statements and have been really losing sleep over it. Here is the problem: I have csv files in the following pipe-delimited format: Column1|Column2|Column3|Column4|NEWLINE Address Type|some descriptive... (4 Replies)
Discussion started by: khayal
4 Replies

6. Shell Programming and Scripting

awk: Transpose csv row to column.

Hello, am I new to awk, and I am tryint to: INPUT FILE: "73423555","73423556","73423557","73423558","73423559" OUTPUT FILE: 73423555 73423556 73423557 73423558 73423559 My useless code so far: #!/bin/awk -F ',' BEGIN { i=0; } (8 Replies)
Discussion started by: drbiloukos
8 Replies

7. Shell Programming and Scripting

Error in fetching multiple row values into variables

Hello Team, In the below code....The variabe values are not fetch from input table into SELECT statements. =========================== #!/usr/bash DATABASE=XXXXX inputFILE=$1 db2 connect to $DATABASE TABLENAME=`echo $inputFILE|awk '{print $1}'` COLUMNNAME=`echo $inputFILE|awk... (2 Replies)
Discussion started by: rocking77
2 Replies

8. UNIX for Dummies Questions & Answers

Delete first row of csv file

I have a csv file, which is > 2 Gigs. I need to BCP that file to Sybase db , but I cant upload that b'caz first row of the file is failing. ( having some errors probably.) I can manually insert the first line into db & then I can upload the rest of the data in file, if i can delete the first row. ... (2 Replies)
Discussion started by: kedar.mehta
2 Replies

9. Shell Programming and Scripting

manipulating csv to add extra row

hi how do i manipulate .csv file to add an extra row after each row using shell script? I need a blank line added for each 1000 records in my file? I will then need to copy and paste some data in the blank row created. thanks 4 ur support neil (3 Replies)
Discussion started by: neil546
3 Replies

10. Shell Programming and Scripting

first row data into variables

I have a datafile having structure like col1,col2,col3 col1,col2,col3,col4,col5 col1,col2,col3,col4,col5 ..... Can we take only first row of values from datafile and put into respective variable. How col1 can be captured in a variable called v_col1 from unix script. like first row(only)... (1 Reply)
Discussion started by: u263066
1 Replies
Login or Register to Ask a Question