![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 12:07 AM |
| format output | Tornado | Shell Programming and Scripting | 7 | 11-19-2006 02:17 AM |
| Format the output of file | getdpg | Shell Programming and Scripting | 9 | 01-24-2006 08:50 AM |
| ls output format | tonyt | UNIX for Dummies Questions & Answers | 6 | 11-23-2001 07:31 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
|||
|
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..... |
|||
| Google The UNIX and Linux Forums |