Formatting data to put it in the excel file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Formatting data to put it in the excel file
# 1  
Old 09-21-2015
Formatting data to put it in the excel file

Hello,

I have a file with the below contents :

Code:
Policy Name:       Backup_bkp

  Policy Type:         Catalog_bkp
  Active:              yes
  Effective date:      08/07/2013 02:02:12
  Mult. Data Streams:  no
  Client Encrypt:      no
  Checkpoint:          no
  Policy Priority:     0
  Max Jobs/Policy:     1
  Disaster Recovery:   0
  Collect BMR info:    no
  Residence:           xyz
  Volume Pool:         Backup
  Server Group:        *ANY*
  Keyword:             (none specified)
  Data Classification:       -
  Residence is Storage Lifecycle Policy:    no
  Application Discovery:      no
  Discovery Lifetime:      0 seconds
ASC Application and attributes: (none defined)

------------------------------------------------------------

Policy Name:       002

  Policy Type:         MS-Windows
  Active:              yes
  Effective date:      07/15/2013 22:26:40
  Backup network drvs: no
  Collect TIR info:    no
  Mult. Data Streams:  no
  Client Encrypt:      no
  Checkpoint:          yes
       Interval:       15
  Policy Priority:     0
  Max Jobs/Policy:     Unlimited
  Disaster Recovery:   0
  Collect BMR info:    no
  Residence:           dfg
  Volume Pool:         NetBackup
  Server Group:        *ANY*
  Keyword:             (none specified)
  Data Classification:       -
  Residence is Storage Lifecycle Policy:    no
  Application Discovery:      no
  Discovery Lifetime:      0 seconds
ASC Application and attributes: (none defined)

Here the record separator (RS) is "------------------------------------------------------------" .I want to format the data and put it in the excel in the following way :

Code:
Policy Name      Policy Type    Active   Effective date                   ...other columns
Backup_bkp     Catalog_bkp   yes      08/07/2013 02:02:12      ...other columns
002                 MS-Windows   yes      07/15/2013 22:26:40     ...other columns


I further did the following :

Code:
 awk 'BEGIN { RS="------------------------------------------------------------";FS="\n";OFS=",";ORS="\n"} { print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11}' script4_tmp2.txt

Code:
Policy Name:       Backup_bkp,  Policy Type:         Catalog_bkp,  Active:              yes,  Effective date:      08/07/2013 02:02:12,  Mult. Data Streams:  no,  Client Encrypt:      no,  Checkpoint:          no,  Policy Priority:     0,  Max Jobs/Policy:     1,  Disaster Recovery:   0,  Collect BMR info:    no
,Policy Name:       002,  Policy Type:         MS-Windows,  Active:              yes,  Effective date:      07/15/2013 22:26:40,  Backup network drvs: no,  Collect TIR info:    no,  Mult. Data Streams:  no,  Client Encrypt:      no,  Checkpoint:          yes,       Interval:       15

Could someone please help me.

Thanks
Rahul

Last edited by rahul2662; 09-21-2015 at 08:14 AM..
# 2  
Old 09-21-2015
Any attempts from your side?
# 3  
Old 09-21-2015
I first removed all the blank lines using : sed -i '/^$/d'

Then I tried to use this :

Code:
awk 'BEGIN { RS="------------------------------------------------------------";FS="\n";ORS="\n";OFS="," } { print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11}' file.txt

Code:
Policy Name:       Backup_bkp   Policy Type:         Catalog_bkp   Active:              yes   Effective date:      08/07/2013 02:02:12   Mult. Data Streams:  no   Client Encrypt:      no   Checkpoint:          no   Policy Priority:     0   Max Jobs/Policy:     1   Disaster Recovery:   0   Collect BMR info:    no
 Policy Name:       002   Policy Type:         MS-Windows   Active:              yes   Effective date:      07/15/2013 22:26:40   Backup network drvs: no   Collect TIR info:    no   Mult. Data Streams:  no   Client Encrypt:      no   Checkpoint:          yes        Interval:       15

I futher did this :

Code:
awk 'BEGIN { RS="------------------------------------------------------------";FS="\n";OFS=",";ORS="\n"} { print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11}' script4_tmp2.txt

Code:
Policy Name:       Backup_bkp,  Policy Type:         Catalog_bkp,  Active:              yes,  Effective date:      08/07/2013 02:02:12,  Mult. Data Streams:  no,  Client Encrypt:      no,  Checkpoint:          no,  Policy Priority:     0,  Max Jobs/Policy:     1,  Disaster Recovery:   0,  Collect BMR info:    no
,Policy Name:       002,  Policy Type:         MS-Windows,  Active:              yes,  Effective date:      07/15/2013 22:26:40,  Backup network drvs: no,  Collect TIR info:    no,  Mult. Data Streams:  no,  Client Encrypt:      no,  Checkpoint:          yes,       Interval:       15

I am not getting proper format.

Thanks
Rahul

Last edited by rahul2662; 09-21-2015 at 08:17 AM..
# 4  
Old 09-21-2015
Do you want <TAB> chars as separators, or better commas as used in .csv (comma separated values) files? Try
Code:
awk -F: '
BEGIN                   {HD="Policy Name,Policy Type,Active,Effective date,Mult. Data Streams,Client Encrypt,Checkpoint,"\
                                "Policy Priority,Max Jobs/Policy,Disaster Recovery,Collect BMR info,Residence,Volume Pool,"\
                                "Server Group,Keyword,Data Classification,Residence is Storage Lifecycle Policy,"\
                                "Application Discovery,Discovery Lifetime,ASC Application and attributes"
                         print HD
                         HDCnt=split(HD,HDArr,",")
                         NXTREC=HDArr[1]
                         HDCM=","HD","
                        }

                        {sub (/^[\t ]*/, "", $1)
                        }

$1 == NXTREC && PR      {for (i=1; i<=HDCnt; i++) printf "%s,", RES[HDArr[i]]
                         printf "\n"
                         delete RES
                        }
$1 == NXTREC            {PR=1}
HDCM ~ "," $1 ","       {RES[$1]=$0; sub ($1 "[^:]*:[\t ]*", "", RES[$1]); sub (/[\t ]*$/, "", RES[$1])}

END                     {for (i=1; i<=HDCnt; i++) printf "%s,", RES[HDArr[i]]
                         printf "\n"
                        }
' OFS=":" file
Policy Name,Policy Type,Active,Effective date,Mult. Data Streams,Client Encrypt,Checkpoint,Policy Priority,Max Jobs/Policy,Disaster Recovery,Collect BMR info,Residence,Volume Pool,Server Group,Keyword,Data Classification,Residence is Storage Lifecycle Policy,Application Discovery,Discovery Lifetime,ASC Application and attributes
Backup_bkp,Catalog_bkp,yes,08/07/2013 02:02:12,no,no,no,0,1,0,no,xyz,Backup,*ANY*,(none specified),-,no,no,0 seconds,(none defined),
002,MS-Windows,yes,07/15/2013 22:26:40,no,no,yes,0,Unlimited,0,no,dfg,NetBackup,*ANY*,(none specified),-,no,no,0 seconds,(none defined),

This User Gave Thanks to RudiC For This Post:
# 5  
Old 09-21-2015
Thanks a Lot RudiC. Can you please explain the script.
# 6  
Old 09-21-2015
Some modifications ... see
Code:
awk -F: '
NR==1                   {HD="Policy Name,Policy Type,Active,Effective date,Mult. Data Streams,Client Encrypt,Checkpoint,"\
                                "Policy Priority,Max Jobs/Policy,Disaster Recovery,Collect BMR info,Residence,Volume Pool,"\
                                "Server Group,Keyword,Data Classification,Residence is Storage Lifecycle Policy,"\
                                "Application Discovery,Discovery Lifetime,ASC Application and attributes"

                         gsub(",", SEP, HD)                     # set up header (and sequence of field printout as well); switch to SEP
                         print HD                               # print it
                         HDCnt=split(HD, HDArr, SEP)            # HDArr n HDCnt needed later for extracting and printing
                         HD=SEP HD SEP
                        }

function PRT()          {DL=""                                  # clear delimiter

                         for (i=1; i<=HDCnt; i++)       {printf "%s%s", DL, RES[HDArr[i]]       # print fields in sequence, plus delimiter
                                                         DL=SEP                                 # set delimiter
                                                        }
                         printf "\n"
                         delete RES                             # clear for next record
                        }

                        {sub (/^[\t ]*/, "")                    # remove leading
                         sub (/[\t ]*$/, "")                    # and trailing white space
                        }

$1 == HDArr[1]          {if (PR) PRT()                          # first field found means: print last complete record, except for the first one
                         PR=1                                   #
                        }

HD ~ SEP $1 SEP         {T=$0                                   # prepare output line 
                         sub ($1 "[^:]*:[\t ]*", "", T)         # remove field name plus space plus colon
                         sub (/[\t ]*$/, "", T)                 # plus trailing space
                         RES[$1]=T                              # save it for print
                        }

END                     {PRT()}                                 # print last record
' SEP="," file


Last edited by RudiC; 09-23-2015 at 03:53 PM..
This User Gave Thanks to RudiC For This Post:
# 7  
Old 09-22-2015
Hello RudiC , can you please explain the below part. How are the second , third and rest of the records printed. I need to understand this to design my script and fix the issues please help :
Code:
$1 == HDArr[1]          {if (PR) PRT()                          # first field found means: print last complete record, except for the first one
                         PR=1                                   #
                        }

HD ~ SEP $1 SEP         {T=$0                                   # prepare output line
                         sub ($1 "[^:]*:[\t ]*", "", T)         # remove field name plus space plus colon
                         sub (/[\t ]*$/, "", T)                 # plus trailing space
                         RES[$1]=T                              # save it for print
                        }

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Visit site get data and put in file for use.

I need to go to "vpnbook"vpnbook.com on the web (can't put in name yet)and open "Openvpn" tab On that page I need to get the username:vpnbook and the next line password:???????? I need to put those two in line one and two of a file "pwfile" When I have those I need to open openVPN with the... (1 Reply)
Discussion started by: tytower
1 Replies

2. Shell Programming and Scripting

Script to generate Excel file or to SQL output data to Excel format/tabular format

Hi , i am generating some data by firing sql query with connecting to the database by my solaris box. The below one should be the header line of my excel ,here its coming in separate row. TO_CHAR(C. CURR_EMP_NO ---------- --------------- LST_NM... (6 Replies)
Discussion started by: dani1234
6 Replies

3. Shell Programming and Scripting

Data formatting in CSV file to EXCEL

Hello friends I want to convert an csv file on unix (which is generated by a ETL application) to a formatted excel sheet like .I have roughly like 28 columns 1)All numbers need to be stored as numbers with leading zeros-like format as text for this column to preserve leading zeroes e.g... (6 Replies)
Discussion started by: etldev
6 Replies

4. Shell Programming and Scripting

Writing excel file using perl : Excel file formatting changed

I am trying to create a program where user can input data in certain excel cells using user interface on internet....the programming is on perl and server is unix But when i parse data into excel the formatting of sheets is turned to default and all macro coding removed. What to do...Please... (7 Replies)
Discussion started by: mud_born
7 Replies

5. UNIX for Advanced & Expert Users

put data in excel file using shell script

Hi. I wish to add data in a specific excel file on daily basis.However the currect dat's data should always come on top i.e for example should always occupy cell A7,B7,C7 .. and the data of day before which was earlier on 7th row of each coloumn should move to 8th row..data on 8th row should... (1 Reply)
Discussion started by: kanus
1 Replies

6. Shell Programming and Scripting

how to put data using shell script to a excel file

Hi, Can any one tell me how to put data using shell script to a excel file from text file to other columns of excel file,leaving first column unaffected i.e it should not overwrite data in first column. Say my text file data is: 15-dec-2008 15-dec-2009 16-dec-2008 16-dec-2009 say my first... (1 Reply)
Discussion started by: siri_886
1 Replies

7. UNIX for Advanced & Expert Users

filter last 24 hour data and put in new file

i have file server 1 (filesvr01acess.log) and disc server 1 (discsvr01acess.log) in unix box(say ip adress of the box 10.39.66.81) Similiarly i have file server 2 (filesvr01acess.log) and disc server 2(discsvr01acess.log) in another unix box(say ip adress of the box 10.39.66.82). Now my... (1 Reply)
Discussion started by: nripa1
1 Replies

8. Shell Programming and Scripting

Copying data from excel file

Hii friends, I am a newbie to unix/shell scripting and got stuck in implementing a functionality.Dear experts,kindly spare some time to bring me out of dark pit :confused:.. My requirement is somewhat wierd,let me explain what i have and what i need to do... 1) there are several excel... (1 Reply)
Discussion started by: 5ahen
1 Replies

9. Shell Programming and Scripting

how to copy data to to excel file

Hi, Can any one tell me how to copy data using shell script to a excel file from text file to other columns of excel file,leaving first column unaffected i.e it should not overwrite data in first column. Say my text file data is: 15-dec-2008 15-dec-2009 16-dec-2008 16-dec-2009 ... (7 Replies)
Discussion started by: tucs_123
7 Replies

10. Filesystems, Disks and Memory

Formatting excel file

Hi, I have a problem in formatting the excel file from my script. Assume that there is a text file generated using a 'C shell Unix script' with a format similar to the one seen below. Each column has a fixed length and every row is going to have a value against a column depending on the... (1 Reply)
Discussion started by: subra
1 Replies
Login or Register to Ask a Question