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
Data File Processing Help mavsman UNIX for Dummies Questions & Answers 5 03-27-2008 04:49 PM
urgent-extracting block data from flat file using shell script shirish_cd Shell Programming and Scripting 4 02-06-2008 09:05 AM
Help with Data Positioning from Columns in a flat file. oott1 Shell Programming and Scripting 3 06-14-2006 01:22 PM
How to compare two flat files and get changed data jtshashidhar Shell Programming and Scripting 3 01-29-2006 10:26 PM
How to compare data in two flat files and update them? rajus19 Shell Programming and Scripting 3 11-08-2005 11:13 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 01-24-2003
wolkott wolkott is offline
Registered User
  
 

Join Date: Sep 2002
Location: Connecticut
Posts: 2
Post processing data in a flat file

Hi there,
i'm quite new to unix scripint hence this thread.. i am trying to write a script that will read a comma delimited flat file one row at a time and write the data into a unix array at the end of the processing, i want to loop through the array and perform the necessary processing..

for example if my file in is the format ;

AAASDGH00000000,1HHHHJ0000000,QWWkjl0000000
SSSDDDD00000000,2395570000000,JKLIUG00000000


i want to a): cut and store the values that have been padded with zeros in an array without the zeros of course..

at the end of this processing i want to loop through the array of records and perform the necessary processing.. ican someone out there point in the right direction ? i'm currently reaching and that ain't doin any good..

any help will be greatly appreciated..

Regards
Wolkott
  #2 (permalink)  
Old 01-24-2003
auswipe's Avatar
auswipe auswipe is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2001
Location: Wide Awake Wylie, Texas
Posts: 535
Re: processing data in a flat file

Quote:
Originally posted by wolkott
Hi there,
i'm quite new to unix scripint hence this thread.. i am trying to write a script that will read a comma delimited flat file one row at a time and write the data into a unix array at the end of the processing, i want to loop through the array and perform the necessary processing..

for example if my file in is the format ;

AAASDGH00000000,1HHHHJ0000000,QWWkjl0000000
SSSDDDD00000000,2395570000000,JKLIUG00000000


i want to a): cut and store the values that have been padded with zeros in an array without the zeros of course..

at the end of this processing i want to loop through the array of records and perform the necessary processing.. ican someone out there point in the right direction ? i'm currently reaching and that ain't doin any good..

any help will be greatly appreciated..

Regards
Wolkott
Take a gander at Perl. Perl was built with this type of processing in mind. You could also use awk for this but I would go with perl.

Some of the concepts that you would need to look up would be arrays, loops and regular expressions.

There are a variety of perl tutorials on the web where you could get started in learning.
  #3 (permalink)  
Old 01-24-2003
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
And for another approach, I would use ksh. (I know, no surprise )...

I wouldn't make one pass to put the data in an array, and then make a pass through the array processing it. I would just process it as it comes. ksh can crack the file into fields if you set IFS to comma in this case. To ksh, "+(0)" is a pattern that matches any number of zeros. And you can use "%%" to strip a pattern off of a string. So here is a start:
Code:
#! /usr/bin/ksh
exec < data.file
IFS=","
while read field1 field2 field3 ; do
       field1=${field1%%+(0)}
       field2=${field2%%+(0)}
       field3=${field3%%+(0)}
       echo $field1 $field2 $field3
done
exit 0
  #4 (permalink)  
Old 01-27-2003
criglerj's Avatar
criglerj criglerj is offline
Registered User
  
 

Join Date: May 2002
Location: Atlanta
Posts: 129
With the Bourne shell (/bin/sh on most systems, /bin/bsh on recent Irixen), you combine the use of IFS and expr to strip trailing zeros off of fields. This gets really slow, though, if your data set is large.
Code:
#!/bin/sh
exec <datafile
while read field1 field2 field3; do
    field1=`expr "$field1" : '\(.*[^0]\)0*$'`
    field2=`expr "$field2" : '\(.*[^0]\)0*$'`
    field3=`expr "$field3" : '\(.*[^0]\)0*$'`
    echo $field1 $field2 $field3
done
You weren't very specific about the kind of processing you need to do with the data after the zeros have been stripped --- you mention the need for an array, but don't say why.

Note that with both the Korn shell and the Bourne shell solution, the output of the while loop can be piped to other filters, e.g.,
Code:
while x; do
    [...]
done | sort
  #5 (permalink)  
Old 01-27-2003
criglerj's Avatar
criglerj criglerj is offline
Registered User
  
 

Join Date: May 2002
Location: Atlanta
Posts: 129
Continuing the thought of additional piping from my previous post, the easiest way to just split and strip is awk. Oh, and awk (and perl) remove the restriction inherent in both shell solutions, namely, you need to have the same number of fields on each line (or do some programming around it, left as an exercise to the reader --- I've always wanted to write that! )
Code:
<datafile awk -F, '{for (i=1;i<=NF;i++) {
    sub("0*$","",$i); print $i } }' | otherprocessing
The perl solution is quite similar:
Code:
<datafile perl -F, -lane 'foreach $x (@F) {
      $x =~ s/0*$//; print $x }' | otherprocessing
The ruby is also quite similar:
Code:
<datafile ruby -F, -lane '$F.each do |x|
m=%r/(.*[^0])0*$/.match x; print m[1]; end' | otherprocessing
As auswipe pointed out, you can do a good deal more processing in perl or ruby as well. Or in awk, for that matter, though it's a little less liberal in your choice of other internal processing.
Closed Thread

Bookmarks

Tags
regex, regular expressions

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 10:35 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