orno
November 13, 2007, 9:33am
1
Hi There!
I have the following block of text in my input file
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
and in order to parse it correctly, i need to have
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
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!
V3l0
November 13, 2007, 10:35am
2
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.
orno
November 13, 2007, 10:53am
4
thanks guys!
both solutions work perfectly, although i think i'll use radoulov's coz its quite quite fast in comparison
cheers!
V3l0
November 13, 2007, 11:00am
5
hi,
Try this one.
coe:
awk '
BEGIN{FS=","}
{
if ($1!=0 && $1+0==0)
print substr($0,index($0,",")+1)
else
print
}' filename