String manipulation

Hi There!

I have the following block of text in my input file

and in order to parse it correctly, i need to have

i.e. If a line starts with a number, ignore it else replace it with the number from the previous line until the first ','

So, for example,
15,9:5/12345
,10:1

becomes
15,9:5/12345
15,10:1

What is the easiest way to do this using standard unix commands?

Thanks in advance!

First file with data (file.dat):

10,9:12/123456789
,10:1
11,10:60
15,9:5/12345
,10:1
,14:1001
17,9:12/123456789
,10:1
,11:1

Second file with program (prog):

#!/bin/ksh

curval=""

while read line
do
        val=$(echo $line | cut -d, -f1)
        if [ "$val" != "" ]
        then
                curval=$val
                echo $line
        else
                echo ${curval}${line}
        fi
done

Use the command:

./prog < file.dat

The result is:

10,9:12/123456789
10,10:1
11,10:60
15,9:5/12345
15,10:1
15,14:1001
17,9:12/123456789
17,10:1
17,11:1
awk '!$1{$1=v}v=$1' FS="," OFS="," filename

Use nawk or /usr/xpg4/bin/awk on Solaris.

thanks guys!
both solutions work perfectly, although i think i'll use radoulov's coz its quite quite fast in comparison :slight_smile:

cheers!

Yes, awk is a very good tool, but it's syntax is ... :eek::eek::eek:

Why make simple when one can make complicated? :cool:

hi,

Try this one.

coe:

awk  '
BEGIN{FS=","}
{
if ($1!=0 && $1+0==0)
print substr($0,index($0,",")+1)
else
print
}' filename