![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | 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 and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Convert XML file into TEXT file using PERL seript | Rudro | Shell Programming and Scripting | 0 | 06-11-2008 10:03 AM |
| how to convert Fixed length file to delimited file. | satyam_sat | Shell Programming and Scripting | 7 | 04-03-2008 02:41 AM |
| Need help to convert Flat file to HTML | getdpg | Shell Programming and Scripting | 11 | 12-20-2006 02:09 AM |
| Plz Help To convert xml file to text file using bourn shell scripts | ram2s2001 | Shell Programming and Scripting | 0 | 11-09-2005 09:56 AM |
| Converting Pivot file to flat file | vskr72 | Shell Programming and Scripting | 2 | 10-18-2005 04:41 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Awk to convert a flat file to CSV file
Hi ,
I have a file with contents as below: Code:
Contract Cancellation Report UARCNCL LOS CODE DATE REAS TYPE AMOUNT AMOUNT LETTER BY ========= ======= ==== ==== ==== ========= ==== ==== 8174739 7641509 1S NONE CRCD 30-JUN-2008 NPAR N .00 .00 CCAN 8678696 8091709 1S NONE DDEB 30-JUN-2008 MVD P .00 .00 LOVES1 8198258 7663210 1S NONE GIRO 30-JUN-2008 NPAR N .00 .00 CCAN 1) remove the lines that end with UARCNCL, remove the lines that start with LOS. 2) In rest of the lines, the fields should be comma separated and written to a new file. the new file should be like: Code:
8174739,7641509,1S,NONE,CRCD,30-JUN-2008,NPAR,N,.00,.00,CCAN 8678696,8091709,1S,NONE,DDEB,30-JUN-2008,MVD,P,.00,.00,LOVES1 8198258,7663210,1S,NONE,GIRO,30-JUN-2008,NPAR,N,.00,.00,CCAN Code:
awk '{if ($4 != 'UARCNCL') print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12}' new > new1
Thanks in advance. Thanks, kumudha Last edited by radoulov; 07-10-2008 at 05:20 AM.. Reason: please use code tags |
|
||||
|
Hope this will be helpful
Code:
$ cat csv.awk
{
if($NF=="UARCNCL" || $1=="LOS" || $1=="=========") next;
gsub(" ",",");
print
}
$ cat test.txt
Contract Cancellation Report UARCNCL
LOS CODE DATE REAS TYPE AMOUNT AMOUNT LETTER BY
========= ======= ==== ==== ==== ========= ==== ====
8174739 7641509 1S NONE CRCD 30-JUN-2008 NPAR N .00 .00 CCAN
8678696 8091709 1S NONE DDEB 30-JUN-2008 MVD P .00 .00 LOVES1
8198258 7663210 1S NONE GIRO 30-JUN-2008 NPAR N .00 .00 CCAN
$ awk -f csv.awk test.txt
8174739,7641509,1S,NONE,CRCD,30-JUN-2008,NPAR,N,.00,.00,CCAN
8678696,8091709,1S,NONE,DDEB,30-JUN-2008,MVD,P,.00,.00,LOVES1
8198258,7663210,1S,NONE,GIRO,30-JUN-2008,NPAR,N,.00,.00,CCAN
Last edited by radoulov; 07-10-2008 at 05:21 AM.. Reason: please use code tags |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|