Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

read from file: a faster way

32 views
Skip to first unread message

ice

unread,
Aug 23, 2012, 4:46:24 AM8/23/12
to
hi all!
I'm newbe in shell programming
My simple script works well but is slow on big file... any suggestion to make it faster?
thanks


this is an example of my input text file:
21/08/2012;L1;t1;t2;t3;t4;t5;
22/08/2012,L2,t1,t2,
23/08/2012;L3;t1;;t3;


This my script:
delim=";"
while read line
do
tokens=$(echo $line|tr -dc $delim|wc -c)
if (($tokens>0));
then
for ((i=1; i<=$tokens; i++))
do
echo [`echo "$line"|cut -d$delim -f$i`]
done
echo .
fi
done <"test.txt"


And this is its output:
[21/08/2012]
[L1]
[t1]
[t2]
[t3]
[t4]
[t5]
.
[23/08/2012]
[L3]
[t1]
[]
[t3]
.

Janis Papanagnou

unread,
Aug 23, 2012, 5:27:08 AM8/23/12
to
Am 23.08.2012 10:46, schrieb ice:
> hi all!
> I'm newbe in shell programming
> My simple script works well but is slow on big file... any suggestion to make it faster?

Yes. In shell, avoid all your unnecessary processes (like cut,
`...`, tr, wc), especially inside loops, and use shell builtins
instead. Yet better, use an appropriate tool for your task; awk
for example.

awk -F\; 'NF>1{for(i=1;i<NF;i++) printf "[%s]\n",$i; print "."}' test.txt


Janis

Lem Novantotto

unread,
Aug 23, 2012, 8:38:06 AM8/23/12
to
ice ha scritto:

> any suggestion

Quite a bit slower than with awk, but these are with sed:

lem@biggy:/tmp$ sed -e '/;/ !d' -e 's/;[^;]*$/;./' -e 's/;/\
/g' <test.txt | sed 's/^[^.]*$/[&]/'

or:

lem@biggy:/tmp$ sed -e '/;/ !d' -e 's/[^;]*/[&]/g' -e 's/\[[^][]*\]$/./' -e 's/;/\
/g' <test.txt

(At the end of the first lines you press ENTER).
--
Bye, Lem
Ceterum censeo ISLAM esse delendum
_________________________________________________________________
Non sprecare i cicli idle della tua CPU, né quelli della tua GPU.
http://www.worldcommunitygrid.org/index.jsp
http://www.rnaworld.de/rnaworld/ http://home.edges-grid.eu/home/
http://www.gpugrid.net/

ice

unread,
Aug 24, 2012, 3:27:35 PM8/24/12
to
Il giorno giovedì 23 agosto 2012 11:27:08 UTC+2, Janis Papanagnou ha scritto:

> awk -F\; 'NF>1{for(i=1;i<NF;i++) printf "[%s]\n",$i; print "."}' test.txt

with sed the work can be done but with awk the processing speed is impressive!!
thank you both guys ;)

-ice-
0 new messages