The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 03:56 AM
Output in a particular format using AWK Raynon Shell Programming and Scripting 4 01-24-2007 04:07 AM
format output Tornado Shell Programming and Scripting 7 11-19-2006 06:17 AM
Format the output of file getdpg Shell Programming and Scripting 9 01-24-2006 12:50 PM
ls output format tonyt UNIX for Dummies Questions & Answers 6 11-23-2001 11:31 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-02-2008
velappangs velappangs is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 2
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
  #2 (permalink)  
Old 04-03-2008
helper helper is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 25
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.....
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0