The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Getting remote data through shell script armohans Shell Programming and Scripting 1 04-15-2008 05:07 PM
Help writing shell script in c++ format deadleg Shell Programming and Scripting 2 03-07-2008 06:08 AM
extract data from xml- shell script using awk nishana Shell Programming and Scripting 5 07-16-2007 06:20 AM
format csv file using shell script raosurya UNIX for Advanced & Expert Users 3 06-27-2007 05:57 AM
Pipe data to shell script tomjones07 Shell Programming and Scripting 3 03-14-2007 03:50 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-31-2008
Registered User
 

Join Date: Jan 2008
Posts: 5
Stumble this Post!
Shell script to format a .CSV data

Hi There

I needed to write a Unix shell script which will pick up the data from a .CSV file and reformat it as per the requirement and write it to another .CSV file.

Currently I am in the proess of Data Import to "Remedy System" (A one kind of incident mangement Application) and this application accepts the Data in a particular format, Hence I want to accomplish this task with the Script without manual intervension.

My Original data loooks some what like this,

AcquiredMethod, AssetID, AssetLifecycleStatus, SC1, SC2, DC1, DC2
Vendor Owned, ATESTIMPORT10, Down, a, b, c, d

This has to be re-formatted like this

AcquiredMethod, AssetID, AssetLifecycleStatus, C, S/D, 1or2
Vendor Owned, ATESTIMPORT10, Down, a, S, 1
Vendor Owned, ATESTIMPORT10, Down, b, S, 2
Vendor Owned, ATESTIMPORT10, Down, c, D, 1
Vendor Owned, ATESTIMPORT10, Down, d, D, 2


I know I am a bad in explaining, however if you go through the excel it will be little clear.

The condition is if there is no data in any of the 4 columns (SC1 or SC2 or SC3 or SC4) the same record will not be present in the out CSV file.

Can anyone please give the script for the above task. I would really appreciate that.

Thanks in Advance,
Uday
Attached Files
File Type: txt Task.txt (1.5 KB, 8 views)
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 01-31-2008
rikxik's Avatar
Registered User
 

Join Date: Dec 2007
Posts: 104
Stumble this Post!
Code:
$ cat in.txt
AcquiredMethod, AssetID, AssetLifecycleStatus, SC1, SC2, DC1, DC2
Vendor Owned, ATESTIMPORT10, Down, a, b, c, d
$
$
$ cat sply.sh
#!/usr/bin/ksh

nawk -F"," 'BEGIN{OFS=","}{ if (NR==1) {
                        print "AcquiredMethod, AssetID, AssetLifecycleStatus, C, S/D, 1or2"
                } else {
                        print $1,$2,$3,$4,"S",1
                        print $1,$2,$3,$5,"S",2
                        print $1,$2,$3,$6,"D",1
                        print $1,$2,$3,$7,"D",2
                }}' in.txt



$
$
$ ./sply.sh
AcquiredMethod, AssetID, AssetLifecycleStatus, C, S/D, 1or2
Vendor Owned, ATESTIMPORT10, Down, a,S,1
Vendor Owned, ATESTIMPORT10, Down, b,S,2
Vendor Owned, ATESTIMPORT10, Down, c,D,1
Vendor Owned, ATESTIMPORT10, Down, d,D,2
HTH
Reply With Quote
  #3 (permalink)  
Old 02-01-2008
Registered User
 

Join Date: Jan 2008
Posts: 5
Stumble this Post!
Thank you

Hi

Thanks!!. That was very quick and it works 95% correct. Only exception here is it create a record in every instance.., i.e. even if SC1 or SC2 or DC1 or DC2 is blank.

I mean we tested this script in all the possibalities, of SC1,SC2,SC3 and SC4, i.e. with all the 16 possibalities and it created total 64 records, where as it should have created only 32. Hence we need to eliminate the other 32 records.

Could you advice on this.

Once again thank you very much for your swift and accurate reply.


Thanks
Uday
Reply With Quote
  #4 (permalink)  
Old 02-01-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/florida
Posts: 954
Stumble this Post!
You mention SC3 and SC4 but the data example you gave is:

AcquiredMethod, AssetID, AssetLifecycleStatus, SC1, SC2, DC1, DC2

BY DC1 and DC2 do you mean SC3 and SC4
Reply With Quote
  #5 (permalink)  
Old 02-01-2008
Registered User
 

Join Date: Jan 2008
Posts: 5
Stumble this Post!
Sorry! you are right

Yes I am sorry you are right


.. It is DC1 and DC2

uday
Reply With Quote
  #6 (permalink)  
Old 02-01-2008
rikxik's Avatar
Registered User
 

Join Date: Dec 2007
Posts: 104
Stumble this Post!
I would prefer that you post some examples - provide sample input and expected output. Cover the cases you mentioned in your samples - that makes is easier and saves our time.
Reply With Quote
  #7 (permalink)  
Old 02-03-2008
Registered User
 

Join Date: Jan 2008
Posts: 5
Stumble this Post!
Here is the example

Hi

Please refer to the attachement attached to with this reply to see an example and all the explanation regarding this. Expecting your help with this.

Thanks and Regards
Uday
Attached Files
File Type: zip Test.zip (9.8 KB, 8 views)
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:33 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0