How can I add a header to a csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I add a header to a csv file
# 1  
Old 10-16-2008
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'`
NNNN="hello"
RR=`wc -l ../xxxx/"xxxxx.txt"`

fixed header fields
1
1
4

How can i get the above in a header and onto the already existing csv file?Smilie
# 2  
Old 10-16-2008
Assuming that you CSV file only uses commas to seperate the fields ....
Code:
#!/usr/bin/ksh93

OUTFILE_YEAR=`date '+%y'`
DATE=`date '+%d%m%y'`
TIME=`date '+%H:%M:%S'`
NNNN="hello"
RR="RRRRRRRR"            # dummy value

HEADER="$OUTFILE_YEAR,$DATE,$TIME,$NNNN,$RR,1,1,4"

cp csvfile /tmp/csvfile.bak
echo $HEADER > outfile
cat csvfile  >> outfile
mv outfile csvfile

If your CSV file is of the format "field1","field2, .... , "fieldN" then the HEADER line shold be changed to
Code:
HEADER="\"$OUTFILE_YEAR\",\"$DATE\",\"$TIME\",\"$NNNN\",\"$RR\",\"1\",\"1\",\"4\""

# 3  
Old 10-17-2008
I have set it like this but the fileout is just as it was going in. The header.sh does not get appended to the file:

HEADER="\"xxxx$OUTFILE_YEAR$INCREMENT\",\"xxxxx\",\"$DATE\",\"$TIME\",\"1\",\"4\",\"$RR\""



echo $HEADER > ../$CUSTOMER_DIRECTORY/header.sh
cat ../$CUSTOMER_DIRECTORY/header.sh >> ../$CUSTOMER_DIRECTORY/${FILE_OUT_NAME}${OUTFILE_YEAR}${INCREMENT}.csv

Does anyone know why is this not working?Smilie
# 4  
Old 10-17-2008
The header should be prepended - not appended to the existing csv file.

Try the following
Code:
echo $HEADER > new.csv
cat ../$CUSTOMER_DIRECTORY/${FILE_OUT_NAME}${OUTFILE_YEAR}${INCREMENT}.csv >> new.csv
cat new.csv

The first line of new.csv should be the header. The remaining lines should contain the existing csv data,
# 5  
Old 10-17-2008
Data

Hi fpmurphy

i do exactly what you say:

echo $HEADER > ../$CUSTOMER_DIRECTORY/new.csv
cat ../$CUSTOMER_DIRECTORY/${FILE_OUT_NAME}${OUTFILE_YEAR}${INCREMENT}.csv >> ../$CUSTOMER_DIRECTORY/new.csv
cat ../$CUSTOMER_DIRECTORY/new.csv

the output of the above command (new.csv) is:

"xxxx08","xxxxxxx","171008","15:05:39","1","4"," 232 ../xxxx/xxxxx xxxx_080910.txt"

exactly the same as when it goes in??? it just seems to ignore the ../$CUSTOMER_DIRECTORY/${FILE_OUT_NAME}${OUTFILE_YEAR}${INCREMENT}.csv ??? Smilie

see below contents of both files that go in to above command:

contents of new.csv
"xxxx08","xxxxxxx","171008","15:05:39","1","4"," 232 ../xxxx/xxxxx xxxx_080910.txt"

contents of ${FILE_OUT_NAME}${OUTFILE_YEAR}${INCREMENT}.csv"

"","ABC","","12345678","","","","","","","","","","","CH1 7DD"

Why is it not working??

Not that it matters but i am in KSH
# 6  
Old 10-17-2008
That code is the same that I use to add headers and trailers. Verify that you have the input file defined correctly. Echo the file name or do an ls on it. It sounds like it may not be interpreting the variables in the file name correctly.
# 7  
Old 10-20-2008
Sorted the problem I had a command previous to what i was doing here and I was piping from that command into this and the file had not yet been created so it was showing as a blank file.

Works great now, thanks everyone for their help.
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. UNIX for Dummies Questions & Answers

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... (4 Replies)
Discussion started by: vkumbhakarna
4 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. Shell Programming and Scripting

Add column header and row header

Hi, I have an input like this 1 2 3 4 2 3 4 5 4 5 6 7 I would like to count the no. of columns and print a header with a prefix "Col". I would also like to count the no. of rows and print as first column with each line number with a prefix "Row" So, my output would be ... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

6. Shell Programming and Scripting

add header in each file of folder

My files location Dir=/pp/Output/Test/ I have 100 .txt files on above location. I want add header "pt all" on each file. Sample Text file Hi Kalol Jt all q Output i want lt all Kalol Jt all (2 Replies)
Discussion started by: asavaliya
2 Replies

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

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

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

10. Shell Programming and Scripting

Split large file and add header and footer to each file

I have one large file, after every 200 line i have to split the file and the add header and footer to each small file? It is possible to add different header and footer to each file? (1 Reply)
Discussion started by: ashish4422
1 Replies
Login or Register to Ask a Question