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
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

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 07-10-2008
rkumudha rkumudha is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 7
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
With the above text, i have two requirements:
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
I'am new to AWK and read through some stuufs in net and wrote a small script( but it did not work)

Code:
awk '{if ($4 != 'UARCNCL') print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12}' new > new1
Please correct me on the above script or please advice me on how shall i get the two rewuirements done.

Thanks in advance.

Thanks,
kumudha

Last edited by radoulov; 07-10-2008 at 05:20 AM.. Reason: please use code tags
  #2 (permalink)  
Old 07-10-2008
ranjithpr ranjithpr is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 157
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
  #3 (permalink)  
Old 07-10-2008
rkumudha rkumudha is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 7
thanks Ranjith!

But when i run the awk, i get the syntax error at line 3, please help me
  #4 (permalink)  
Old 07-10-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Code:
awk 'NR < 4{next}{gsub(" ",",");print}' file > newfile
With sed it should be something like:

Code:
sed -e '1,3d' -e 's/ /,/g'  file > newfile
Regards
  #5 (permalink)  
Old 07-11-2008
Diabolist Diabolist is offline
Registered User
  
 

Join Date: Mar 2002
Posts: 44
Code:
egrep -v "UARCNCL$|^LOS|^===" inputfile |sed -e 's/ /,/g'
  #6 (permalink)  
Old 07-11-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,420
Or try:
Code:
awk '! /^LOS|UARCNCL$/{gsub(" ",",");print}' file
  #7 (permalink)  
Old 07-17-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
The input data you have pasted doesn't appear to match what you have described.
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 04:15 PM.


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