![]() |
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 |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
|||||
|
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
|
|
|||||
|
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
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
|
|
|||||
|
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
Code:
<datafile perl -F, -lane 'foreach $x (@F) {
$x =~ s/0*$//; print $x }' | otherprocessing
Code:
<datafile ruby -F, -lane '$F.each do |x| m=%r/(.*[^0])0*$/.match x; print m[1]; end' | otherprocessing |
![]() |
| Bookmarks |
| Tags |
| regex, regular expressions |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|