Need heading in files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need heading in files
# 1  
Old 06-15-2015
Need heading in files

Hi All,

The below code is splitting the file "data.csv" into several files based on the"client_code" column.

Code:
date_yyyymmdd=$(my_date "" -e"%Y%m%d")
file_format="_$date_yyyymmdd.csv
awk '{print  $2, $3>  DIR"/clients_" $1 file_out}' FS=' *; *' OFS=";" DIR="$TEMPL" file_out="_$file_format" $TEMPL/data.csv


Code:
data.csv
client_code;contact_name;product_id 
XYZ;AMAR ;AB123456               
ABC;KIRAN;CB789                        
XYZ;RAJ;CS78890                       
ABC;KAMESH;A33535335                                   
XYZ;SOM ;MD6546474777              
XYZ;GANE ;MS657869933553666747

The splitted files are as follows.

Code:
clients_ABC_20150615.csv            
ABC;KIRAN;CB789                                              
ABC;KAMESH;A33535335                                   

clients_XYZ_20150615.csv
XYZ;AMAR ;AB123456                                      
XYZ;RAJ;CS78890                                                          
XYZ;SOM ;MD6546474777              
XYZ;GANE ;MS657869933553666747

clients_client_code_20150615.csv
client_code;contact_name;product_id

The splitted files are not having this heading client_code;contact_name;product_id

I want client_code;contact_name;product_id this heading
in these clients_ABC_20150615.csv and clients_XYZ_20150615.csv splitted files.

Please help me.

Thanks
# 2  
Old 06-15-2015
Try
Code:
awk '
NR==1           {HD = $0; next}
                {FN = DIR"/clients_" $1 file_out
                 if (!(CL[FN]++)) print HD > FN 
                 print > FN}
' FS=' *; *' OFS=";" DIR="$TEMPL" file_out="$file_format" $TEMPL/data.csv


Last edited by RudiC; 06-15-2015 at 10:45 AM..
# 3  
Old 06-15-2015
i,

Thansks a lot for your code.

I am extremely Sorry, I have posted wrong files.
Slight change in the split files, split files do not contain first column "client_code".
The files will be.

Code:
clients_ABC_20150615.csv            
KIRAN;CB789                                              
KAMESH;A33535335                                   

clients_XYZ_20150615.csv
AMAR ;AB123456                                      
RAJ;CS78890                                                          
SOM ;MD6546474777              
GANE ;MS657869933553666747

clients_client_code_20150615.csv
client_code;contact_name;product_id

I have tried your code.

This is giving the headings as below.

client_code;contact_name;product_id

The heading should be contact_name;product_id.

This is coming correctly for this file.

Code:
clients_client_code_20150615.csv
contact_name;product_id

The heading should be "contact_name;product_id."

Code:
clients_ABC_20150615.csv    
contact_name;product_id        
KIRAN;CB789                                              
KAMESH;A33535335                                   

clients_XYZ_20150615.csv
contact_name;product_id
AMAR ;AB123456                                      
RAJ;CS78890                                                          
SOM ;MD6546474777              
GANE ;MS657869933553666747

Please help me.
Thanks,
# 4  
Old 06-15-2015
Change the print > FN to print $2, $3 > FN , then.
And make the first awk script line NR==1 {HD = $2 OFS $3; next}

Are you sure you want the clients_client_code_20150615.csv file? If not, Remove the ; next .
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Joining Columnar heading from 2 lines

Hi, Below is the format of a report generated by a custom reporting solution. I opened the report in Notepad++ and junked data and values as in the image below. https://www.unix.com/attachment.php?attachmentid=7907&stc=1&d=1575507708 I want to convert the values to a PIPE delimited format as... (2 Replies)
Discussion started by: ramkrix
2 Replies

2. Shell Programming and Scripting

SQL Column Heading

Hi, I am selecting few columns from a table using select query in UNIX shell script ans sending the results as E-mail. But the column headings are not displayed fully. Instead it displays only a part of the heading. Please help as how to display it fully. Thanks in advance for your help. (7 Replies)
Discussion started by: pvelmuru
7 Replies

3. UNIX for Dummies Questions & Answers

Using awk to get folder name and date fields with heading

It is on the HP-Unix Output desired: Folder Name Date Folder1 Mar 18 10:00 Folder2 Mar 17 12:23 Tried with ls -ldt wp411* | awk '{print $9,"\t",$6," ",$7," "$8}' Please help me with the headings (3 Replies)
Discussion started by: Siva SQL
3 Replies

4. UNIX for Dummies Questions & Answers

Replacing "." with "GG" in a certain column of a file that has heading

Hi, all, I have a file that looks like: ## XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ## YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY #AA AB AC AD AE AF AG AH AI AJ AK AL 20 60039 60039 ... (5 Replies)
Discussion started by: kush
5 Replies

5. Shell Programming and Scripting

Script to get the heading in files along with data

Hi everyone, I have a file which has data with different heading and column names as below. Static Data Ingested ,,,,,,,,,,,,Known Explained Rejections Column_1,column_2,Column_3,Column_4,,Column_6,Column_7,,% Column_8,,Column_9 ,Column_10... (1 Reply)
Discussion started by: Vivekit82
1 Replies

6. Shell Programming and Scripting

Sqlplus Set Heading not Working

Hello, Could someone tell me why this still returns headings? echo "SELECT columnA from tableA group by columnA;" | sqlplus -s ${DB_CONNECT} set heading OFF | while read line do arr="$line" echo ${arr} let i=$i+1 done (2 Replies)
Discussion started by: flowervz
2 Replies

7. UNIX for Dummies Questions & Answers

Extract columns from multiple files with a file name as heading

Hi All, I have 7 files, each containing 9 columns separated by space. I want to extract the 9th columns from every file and save in a new file. The columns must be pasted next to each other. And the title of each columns should be the name of the corresponding files! Since the 3rd column is... (1 Reply)
Discussion started by: Unilearn
1 Replies

8. Shell Programming and Scripting

SQL sorrting issue, and unwanted blank line above heading

Hi, I am running a shell script on Compaq Tru64 UNIX V5.1A. I have attached the shill script with the sql script it is calling to extract some data(hyp_dta_Extr.sql), and the results. I need the file to be tab delimited. (please note that I have renamed hyp_dta_Extr.sql to hyp_dta_Extr.txt to... (1 Reply)
Discussion started by: dazz
1 Replies

9. UNIX for Dummies Questions & Answers

What is the command to add heading to a file?

Hi, I'd like to add one line to the top of my file. I know there is a command to be able to do it. But I forgot ...... Please help if you can .... (3 Replies)
Discussion started by: whatisthis
3 Replies
Login or Register to Ask a Question