Computing.Net > Forums > Unix > Require help to format data

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Require help to format data

Reply to Message Icon

Name: Venu
Date: January 3, 2005 at 20:00:59 Pacific
OS: HP Unix
CPU/Ram: 1 GB
Comment:

Hi,

Can someone help to write a script for formating data as given below

Input:

Client: shp13
Backup ID: shp13_1104610099
Policy: hp_shp13_app
Creator: root
Sched Label: Weekly
Schedule Type: UBAK (2)
Retention Level: 2 weeks (1)
Backup Time: Sun Jan 02 2005 04:08:19 (1104610099)
Elapsed Time: 5 second(s)
Expiration Time: Sun Jan 16 2005 04:08:19 (1105819699)
Kilobytes: 30620
Number of Files: 11
Keyword: Arch_appvcp

Client: shp13
Backup ID: shp13_1104609818
Policy: hp_shp13_app
Creator: root
Sched Label: Weekly
Schedule Type: UBAK (2)
Retention Level: 2 weeks (1)
Backup Time: Sun Jan 02 2005 04:03:38 (1104609818)
Elapsed Time: 10978 second(s)
Expiration Time: Sun Jan 16 2005 04:03:38 (1105819418)
Kilobytes: 79988740
Number of Files: 648105
Keyword: SHP13PVCS

From client to keyword the data will repeat n number of times.

Out Required:

Client,Backup ID,Policy,Creator,SChed Label,Schedule Type,Retention Level,Backup Time,Esalpsed Time,Expiration Time,Kilobytes,Number of Files,Keyword
shp13,shp13_1104610099,hp_shp13_app,root,Weekly,UBAK (2),2 weeks (1),Sun Jan 02 2005 04:08:19 (1104610099),5 second(s),Sun Jan 16 2005 04:08:19 (1105819699),30620,11,Arch_appvcp
shp13,shp13_1104609818,hp_shp13_app,root,Weekly,UBAK (2),2 weeks (1),Sun Jan 02 2005 04:03:38 (1104609818),10978 second(s),Sun Jan 16 2005 04:03:38 (1105819418),79988740,648105,SHP13PVCS

Perl or Shell/awk will do

Thanks




Sponsored Link
Ads by Google

Response Number 1
Name: David Perry
Date: January 4, 2005 at 04:47:52 Pacific
Reply:

What have you got so far?


0

Response Number 2
Name: David Perry
Date: January 4, 2005 at 06:13:43 Pacific
Reply:

This is a non-working start. It needs the ','.

sed -e 's/:/-/' infile | awk -F '-' 'BEGIN { ORS = '\012' } /Client-/,/Keyword-/ { printf %s,, $2 }'


0

Response Number 3
Name: vgersh99
Date: January 4, 2005 at 12:39:48 Pacific
Reply:

nawk -f venu.awk infile

here's venu.awk

BEGIN {
FS=""
OFS=","
RS=""
}

FNR==1{
for(i=1; i <= NF; i++)
printf("%s%s", substr($i,1,index($i,":")-1), (i==NF) ? "\n" : OFS);
}

{
for(i=1; i <= NF; i++)
printf("%s%s", substr($i,index($i,":")+1), (i==NF) ? "\n" : OFS);
}


0

Response Number 4
Name: thepubba
Date: January 4, 2005 at 17:21:44 Pacific
Reply:

I've provided a Korn shell solution and another poster has provided an nawk solution. If you are doing this as part of your job, I'd suggest you learn how to do this yourself by buying a book. If it is a homework assignment, I'd suggest you read the book.

#!/bin/ksh

OLDIFS=$IFS
NEWIFS=":
"

exec 3<./junk.data99

while read -u3 title data1 data2
do
if [[ $title = "Client" ]]
then
IFS=$NEWIFS
titleLine=""
dataLine=""
titleLine="$titleLine$title,"
dataLine="$dataLine$data1,"
elif [[ $title = "" ]]
then
:
elif [[ $title = "Keyword" ]]
then
IFS=$NEWIFS
titleLine="$titleLine$title"
dataLine="$dataLine$data1"
print $titleLine
print $dataLine
elif [[ $data1 = "Time" ]]
then
IFS=$OLDIFS
titleLine="$titleLine$title"
dataLine="$dataLine$data1$data2"
else
IFS=$NEWIFS
titleLine="$titleLine$title,"
dataLine="$dataLine$data1,"
fi
done

Jerry Lemieux



0

Response Number 5
Name: vgersh99
Date: January 5, 2005 at 05:59:33 Pacific
Reply:

intersting...
So if this's a part of your work - just "BUY a book".
And if this's a homework, then "READ the book".

AM I getting it right?

Sorry - just kiddin'.


0

Related Posts

See More



Response Number 6
Name: thepubba
Date: January 5, 2005 at 06:54:24 Pacific
Reply:

I meant that if it was homework, read the text book. If it was for the job, buy a book (and by extension) read it. I guess I wasn't paying much attention to what I was writing. A long day.


0

Response Number 7
Name: Venu
Date: January 6, 2005 at 04:04:23 Pacific
Reply:

David/Vgersh/Jerry,

You guys are great. Thanks a lot for your help. All of your guidance was of great help.

Cheers



0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Require help to format data

How to do data cleansing in unix www.computing.net/answers/unix/how-to-do-data-cleansing-in-unix/7068.html

script to combine data from 2 files www.computing.net/answers/unix/script-to-combine-data-from-2-files/8101.html

Help re-formating hard drive www.computing.net/answers/unix/help-reformating-hard-drive/1635.html