[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: cat "$infofile" | grep "^string" | sed 's/.*=//'



Op 19-06-2008 om 13:47 schreef Geert Stappers:
> > +		if [ "$usage" = "dhcp" ]; then
> > +			NET_CONFIG=dhcp
> > +		else
> > +			NET_CONFIG=static
> > +			IP_ADDRESS=$(cat "$netinfo" | grep "^my_ipaddress" | sed 's/.*=//')
> > +			NETMASK=$(cat "$netinfo" | grep "^my_subnetmask" | sed 's/.*=//')
> > +			GATEWAY=$(cat "$netinfo" | grep "^my_dgw" | sed 's/.*=//')
> > +		fi
> > +	fi
> > +	hostinfo=$path/rootfs/etc/host.info
> > +	if [ -e "$hostinfo" ]; then
> > +		HOSTNAME=$(cat "$hostinfo" | grep "^hostname" | sed 's/.*=//')
> > +	fi
> 
> 
>    cat "$infofile" | grep "^string" | sed 's/.*=//'
> 
> can be rewritten as
> 
>    grep "^string" "$infofile" | sed 's/.*=//'
> 
> 
> with the advantage of 'cat' not being spawned.
> 
> 
> 
> Further rewrite to
> 
>    sed -n '/^string/s/.*=//' "$infofile" 
Oops, forgot to print
     sed -n '/^string/s/.*=//p' "$infofile" 
> 
> even avoids 'grep' being started.
> 

I don't known how fast (or slow) a KuroBox is.

On a 350MHz Pentium III did I run a speed test

Original version:
real    0m8.269s
user    0m3.256s
sys     0m4.972s

Without cat:
real    0m5.923s
user    0m2.160s
sys     0m3.760s

Only sed:
real    0m2.692s
user    0m1.084s
sys     0m1.608s



Attached the test script

Cheers
Geert Stappers
# execute with sh
infofile=infofile.txt
cat << HERE > $infofile
some line
another line
string=value
more strings
HERE

time for i in $(seq 500)
do
  VAR=$( cat $infofile | grep "^string" | sed 's/.*=//')
done
echo $VAR

time for i in $(seq 500)
do
  VAR=$( grep "^string" $infofile | sed 's/.*=//')
done
echo $VAR

time for i in $(seq 500)
do
  VAR=$( sed -n '/^string/s/.*=//p' $infofile )
done
echo $VAR

Reply to: