Creating a csv file with header in UNIX


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Creating a csv file with header in UNIX
# 1  
Old 04-17-2013
Creating a csv file with header in UNIX

I have a flat file that contains dynamic list of variables like
a=1
b=2
c=3
.
..
z=26

I need to convert the above into a csv file having the format below:
a,b,c,..,z
1,2,3,..,26

Please note, I do not want a comma separating the last variable.

I tried to refer the post 132268-how-create-csv-file-using-shell-script but it does not say how to create a header. (sorry I can't post it's url since I am limited in my ability to post url's as i don't have 5 posts on the forum)

Requesting your help.

Many Thanks,
# 2  
Old 04-17-2013
Code:
awk -F'=' '{f1=f1?f1","$1:$1;f2=f2?f2","$2:$2}END{print f1; print f2}' file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 04-17-2013
Thanks a lot for the immediate response Sir yoda. May the force be with you. Smilie

I am new to unix shell scripting, can you please help explain the code a little bit.
# 4  
Old 04-17-2013
The code uses a Conditional Expression to construct variables: f1 & f2 values

If f1 is defined, then append f1 value with comma , and first field $1 and assign to f1 else assign first field $1. Similarly for f2 but using second field $2.

In the END block, print variable values.
This User Gave Thanks to Yoda For This Post:
# 5  
Old 04-18-2013
Try this ...

suppose that file content lies in a.txt

Code:
fld1=`awk -F'=' '{print $1}' a.txt|tr  '\n' ','|sed s/".$"//`
fld2=`awk -F'=' '{print $2}' a.txt|tr  '\n' ','|sed s/".$"//`
echo $fld1
echo $fld2

This User Gave Thanks to palanisvr For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to validate header in a csv file

Hi All; I am struggling to write a script that validates file header. Header file would be like below with TAB separated TRX # TYPE REF # Source Piece Code Destination Piece Code every time I need to check the txt file if the header was same as above fields if validation success... (6 Replies)
Discussion started by: heye18
6 Replies

2. Shell Programming and Scripting

Matching the header of a .CSV file with dynamic field names

I have a .CSV file (frequency - weekly) whose header contains the year-week value in two of the columns which keeps changing every week. For an instance please see below. Column1,Column2,Column3,Column4,Column5,Column6,Column7,Column8,Column9,Column10,Column11,Column12,Column13,201420... (4 Replies)
Discussion started by: dhruuv369
4 Replies

3. Shell Programming and Scripting

Creating your own Header file in awk

whether it is possible in awk to create your own header file like in C if so someone give me awk equivalent for below one $ cat foo.h int add(int a,int b) { return(a+b); } $ cat main.c #include<stdio.h> #include"foo.h" void main() { int number1=10,number2=10,number3; number3 =... (5 Replies)
Discussion started by: Akshay Hegde
5 Replies

4. Shell Programming and Scripting

How to get sqlplus column header once in csv file?

Hi All, Could anyoone please let me know how do I get sqlplus column header once in csv file Scripts are below: cat concreq.sh #!/bin/bash . $HOME/.profile while ; do sqlplus apps/pwd <<-EOF set lines 100 pages 100 col "USER_CONCURRENT_QUEUE_NAME" format a40; --set termout off... (5 Replies)
Discussion started by: a1_win
5 Replies

5. UNIX for Dummies Questions & Answers

Merge all csv files in one folder considering only 1 header row and ignoring header of all others

Friends, I need help with the following in UNIX. Merge all csv files in one folder considering only 1 header row and ignoring header of all other files. FYI - All files are in same format and contains same headers. Thank you (4 Replies)
Discussion started by: Shiny_Roy
4 Replies

6. Shell Programming and Scripting

Add header to a .csv file

Hi, I am trying to add a header record to all the .csv files in a directory. I am using the below sed commnad sed -i '1 i \abc,sam,xyz,tip,pep,rip' xyz.csv but this is not adding the header and I am not getting any error,pls tell me if any thing is wrong in the code. Thanks, Shruthi (2 Replies)
Discussion started by: shruthidwh
2 Replies

7. Shell Programming and Scripting

Creating Header & Trailer for bulk volume data file

Hi all, I have a requirement to create a Header &Trailer for a file which is having 20 millions of records. If I use the following method, i think it will take more time. cat "Header"> file1.txt cat Data_File>>file1.txt cat "Trailer">>file1.txt since second CAT command has to read all... (4 Replies)
Discussion started by: Raamc
4 Replies

8. Shell Programming and Scripting

Append Header in CSV file

Hi, I create a csv file and the output looks like below Arun,E001 Sathish,E003 Now i need to include the below header and the output should like below Name,Number Arun,E001 Sathish,E003 Please guide me. Thanks (4 Replies)
Discussion started by: Sekar1
4 Replies

9. Shell Programming and Scripting

How can I add a header to a csv file

I have a csv file which has no header. the file has 15 fields and needs to go out with a header of 8 fields. The header content needs to have some variables and some fixed that i have set up: variable header fields OUTFILE_YEAR=`date '+%y'` DATE=`date '+%d%m%y'` TIME=`date '+%H:%M:%S'`... (6 Replies)
Discussion started by: Pablo_beezo
6 Replies

10. Shell Programming and Scripting

creating a csv file in awk

Hi All I am trying to create a csv file in the korn shell and the script segment is as follows: if then # NEED TO ADD INFO TO THE EMAIL FILE ABOUT THE DRIVE THAT'S FILLING UP echo "$drive $percent% $space "|\ awk '{printf("%d/t"|"%d/t"|"%d/t\n",... (6 Replies)
Discussion started by: Segwar
6 Replies
Login or Register to Ask a Question