1834842 Members
2218 Online
110070 Solutions
New Discussion

Re: Optimizing script

 
SOLVED
Go to solution
Ferdi Castro
Advisor

Optimizing script

Hi,

I had this file generated by a script.
The script prompt user to enter the start index then it paste the index in the file.
Lets say file abc has contents
wqer
asdf
zxcv
then I ran the script: enter first index : read index then example index = 1.
then it outputs the file
wqer 1
asdf 2
zxcv 3 if index = 4 then it ouputs file
qwer 4
asdf 5
zxcv 6
Is there a way where I can fasten it up like awk? i just did a loop script but it is slow for 60K rows (would take 20mins)
thanks.
9 REPLIES 9
Steven E. Protter
Exalted Contributor

Re: Optimizing script

Shalom Ferdi,

It would be helpful if you attached your script. I am sure it could be sped up with a number of methods, but to optimize a script, I need to see the script.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Jean-Yves Picard
Trusted Contributor
Solution

Re: Optimizing script

Hello,

read index
awk -v s=$index '{print $0 " " s ; s++}' $myfile

whatever the way, you still have to parse original file.
the only way to speed the process is to insert index before (when you create file at first place)

Jean-Yves Picard
Peter Godron
Honored Contributor

Re: Optimizing script

Ferdi,
Jean-Yves solution with awk runs in less than 1 second on my machine (L1000-44)!
You are redirecting the output ?

read index
awk -v s=$index '{print $0 " " s ; s++}' $myfile > $myfile.new
Indira Aramandla
Honored Contributor

Re: Optimizing script

Hi Ferdi,

You can improve by providing the index value in the awk statement instead of reading as a value.

Just like here.

cat abc | awk '{print $1 " " a+1;a++}'

if your index =1


IA
Never give up, Keep Trying
dirk dierickx
Honored Contributor

Re: Optimizing script

give it a try in perl, it could be faster at it then awk.
Arturo Galbiati
Esteemed Contributor

Re: Optimizing script

Hi,

read index?"start index: "
awk 'BEGIN {start_index='''$index'''};{print $0 " " start_index++}'

HTH,
Art
Michael Schulte zur Sur
Honored Contributor

Re: Optimizing script

Hi Ferdi,

I somewhat do not get what you want to do with that file? You use index=1 and then index=4?

greetings,

Michael
James R. Ferguson
Acclaimed Contributor

Re: Optimizing script

Hi Ferdi:

As mentioned, in the absence of a sample of what you want to achieve, it is difficult to provide optimizations.

What language or languages do you want to use --- Shell, awk, Perl ?

If you are using a shell, 'typeset' to integer for faster arithmetic at the least.

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: Optimizing script


This appears to be a continutation of topic:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1006890

Ferdi,
Please help us understand why the awk solutions where not deemed acceptable. Maybe we can address that.

And if the conslusion is that only a shell script is acceptable, then why not show us what you have now. Maybe someone will sport a critical problem with it.
But I believe we can only add value once we understand why the simple awk (or perl or whatever) solution was not accpetable.
In the mean time, here is a silly sh script which would not take 20 minutes for 60k lines:


# cat > x
wqer
asdf
zxcv
# export index=123
# ./x.sh < x
wqer 124
asdf 125
zxcv 126
# export index=4
# ./x.sh < x
wqer 5
asdf 6
zxcv 7

# cat x.sh

#!/bin/sh
while read line
do
let "index += 1"
echo "$line $index"
done


hth,
Hein.