Computing.Net > Forums > Unix > unix text file parser

unix text file parser

Reply to Message Icon

Original Message
Name: Irene
Date: August 7, 2003 at 01:29:00 Pacific
Subject: unix text file parser
OS: Windows 2000
CPU/Ram: P 4/256 MB
Comment:

Hi all,
I'm working on Exceed (unix machine) from Windows.
With a grep command on a file I build a new text file
in UNIX in wich every line is of type:

X:Y;Z;K;A,B\n


I have to fill an Oracle db table (by SQLLoader*) only with fields
Y - Z - K - A


So before submitting the text file to SQLLoader I must
'clean' it by replacing each line with

Y;Z;K;A\n

How can I build a text file parser in UNIX ?
Which instrument should I use and which extension should have
the command parser ?


Thank You very much,
Irene


Report Offensive Message For Removal


Response Number 1
Name: Dlonra
Date: August 7, 2003 at 14:27:12 Pacific
Reply: (edit)

assuming there are no other ":" or "," on a line, pipe your grep through:
sed -e 's/^.*://' -e 's/,.*//'


Report Offensive Follow Up For Removal

Response Number 2
Name: James Boothe
Date: August 7, 2003 at 15:27:22 Pacific
Reply: (edit)

That sed command will eliminate the extraneous data from your first and fourth fields, but as Dlonra points out, if fields 2 or 3 contain any colons or commas, the sed command chops too much.

The awk code below edits only those two fields, leaving remainder of line along.  It prints each line in csv format in case any of the fields have embedded commas.

awk -F\; '\
{sub(".*:","",$1)
 sub(",.*","",$4)
 printf "\"%s\",\"%s\",\"%s\",\"%s\"\n",$1,$2,$3,$4
}' filein > fileout


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: unix text file parser

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 6 Days.
Discuss in The Lounge