samedi 27 décembre 2014

how to split file and lines


I have a text file like :



1_01 { ; quite good spirals


reset=2000 type=mandel passes=1
corners=-0.6014129278/-0.5990935452/0.427747516/0.429487053
params=0/0 float=y maxiter=1000 inside=0 outside=15
distest=1/10/320/200
}

1_02 { ; stringy one, with dist estimator
reset=2000 type=mandel passes=1
corners=-1.9228429644992/-1.9228427944992/-6.3749991620026e-008/6.375000\
8379971e-008 params=0/0 float=y maxiter=1000 inside=0 outside=15
distest=1/20/320/200
}

1_03 { ; OK, bit dull, not zoomed in far
reset=2000 type=mandel passes=1
corners=0.3734922373/0.3820837907/-0.243292645/-0.23684898
params=0/0 float=y maxiter=1000 inside=0 outside=15
distest=1/10/320/200
}

1_04 { ; a mess, needs dist est
reset=2000 type=mandel passes=1
corners=-1.862224008886682/-1.86222400040936/-3.214020831358832e-009/3.1\
43970347410528e-009 params=0/0 float=y maxiter=1000 inside=0
outside=15 distest=1/10/320/200
}


and I would like to split it into files and lines like :



1_01 { ; quite good spirals
reset=2000
type=mandel
passes=1
corners=-0.6014129278/-0.5990935452/0.427747516/0.429487053
params=0/0
float=y
maxiter=1000
inside=0
outside=15
distest=1/10/320/200
}


Now I have made a bash script :



#!/usr/bin/env bash
# chmod +x s.sh
# ./s.sh
for f in *.txt;
do
echo " found "$f " file ";
#split -l 7 $f;
awk '/{/{n++}{print > n".p" }' $f
echo $f "- split when { is found and add p extension " ;
rm $f;
echo " input file " $f " is removed " ;
done

for f in *.p;
do
echo " in "$f " file replace space with newline and add par extension"
# tr '{}' '()' < infile > outfile
tr ' ' '\n' < $f >$f"ar"
rm $f;
done

for f in *.par;
do
echo "remove blank= empty lines"
sed -i '/^$/d' $f
done


It works , but can I do it better ?



Aucun commentaire:

Enregistrer un commentaire