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
File Format issue: Output of sqlplus deepakgang UNIX for Dummies Questions & Answers 2 10-25-2007 12:56 AM
Output in a particular format using AWK Raynon Shell Programming and Scripting 4 01-24-2007 01:07 AM
format output Tornado Shell Programming and Scripting 7 11-19-2006 03:17 AM
Format the output of file getdpg Shell Programming and Scripting 9 01-24-2006 09:50 AM
ls output format tonyt UNIX for Dummies Questions & Answers 6 11-23-2001 08:31 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 04-02-2008
Registered User
 

Join Date: Apr 2008
Posts: 1
Output format - comparison with I/p file

Hi,

I have a file which contains more than 1 lakh records like following:

a. name, id, city, state, country, phone (Expected I/P file format)
name, id, city,, state, country, phone (Current I/P file format )

I want to achieve following tasks,

a, Remove the extra comma in the City field.
b, Add / Remove extra string in the id field (eg. if id is 5001 I want to add 0 in the beginning, ie, 05001 / in some case, if id is 0123 remove 0 from the beginning ie, 123.

Please advice.

Thanks and Regards,
Vel
Reply With Quote
Forum Sponsor
  #2  
Old 04-03-2008
Registered User
 

Join Date: Mar 2008
Posts: 23
Use SED for doing it

Case 1 : For removing multiple comma's which are together.

sed '1,$s/,,/,/' sourcefile >> targetfile


Case 2 : I understand like this.
If the id is starting with '0' then eliminate it
If its not starting with '0' then add it.

The scenario is pretty tricky...
Lets say we have a file like this
cat test
1001,aa,bb,cc,dd
1001,aa,bb,cc,dd
1001,aa,bb,cc,dd
1001,aa,bb,cc,dd
011,aa,bb,cc,dd
011,aa,bb,cc,dd
011,aa,bb,cc,dd
011,aa,bb,cc,dd
aa,bb,cc,dd
aa,bb,cc,dd
aa,bb,cc,dd
aa,bb,cc,dd
aa,bb,c,d

and if i run like this
sed '1,$s/^[1-9]/0/;1,$s/^0//' test

Check the output..

001,aa,bb,cc,dd
001,aa,bb,cc,dd
001,aa,bb,cc,dd
001,aa,bb,cc,dd
11,aa,bb,cc,dd
11,aa,bb,cc,dd
11,aa,bb,cc,dd
11,aa,bb,cc,dd
aa,bb,cc,dd
aa,bb,cc,dd
aa,bb,cc,dd
aa,bb,cc,dd
aa,bb,c,d

but we didnt need this..
We will have to distinguish between them..
so use this command.
sed '1,$s/^[1-9]/-0&/;1,$s/^0//' test >> test1
cat test1
output
======
-01001,aa,bb,cc,dd
-01001,aa,bb,cc,dd
-01001,aa,bb,cc,dd
-01001,aa,bb,cc,dd
11,aa,bb,cc,dd
11,aa,bb,cc,dd
11,aa,bb,cc,dd
11,aa,bb,cc,dd
aa,bb,cc,,dd
aa,bb,cc,,dd
aa,bb,cc,,dd
aa,bb,cc,,dd
aa,bb,c,d

Now u just have to eliminate the "-"
sed 's/^-//g' test1 >> test2
cat test2
output
=====
01001,aa,bb,cc,dd
01001,aa,bb,cc,dd
01001,aa,bb,cc,dd
01001,aa,bb,cc,dd
11,aa,bb,cc,dd
11,aa,bb,cc,dd
11,aa,bb,cc,dd
11,aa,bb,cc,dd
aa,bb,cc,dd
aa,bb,cc,dd
aa,bb,cc,dd
aa,bb,cc,dd
aa,bb,c,d

Hope i have made this clear.
Let me know if any.....
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:29 PM.


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

Content Relevant URLs by vBSEO 3.2.0