slow command execution?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting slow command execution?
# 1  
Old 02-04-2009
slow for loop command execution?

Dear World,

I just wrote a script, which puzzled me somewhat. The siginficant code was:

Code:
for file in `ls splits*`; # splits* came from a split command executed earlier
 do
 tail -$SomeNumber $file | cut -d" " -f6 > $file;
done;

The interesting thing is this: A few of the $files were written but most were just empty. They should have contained the sixth field from the cut but didn't.

Is this a bug/feature of bash shells? Is some subprocess faster than another? 42?

Btw.: I fixed it by using some prefix to the $file after the cut redirection. It's somewhat annoying though since execution of the script takes 2 secs longer this way (guess its the filesystem i/o).

Cheers,
BG

Last edited by BandGap; 02-04-2009 at 09:31 AM..
# 2  
Old 02-04-2009
Even if the files are empty you're still going to have the overhead of opening reading and closing them, so if it's a lot of files, that may take some time.

Regarding not getting the 6th field. If there is more than one space between each field than using a space as the delimiter to cut can be problematic. If you need to use spaces, I'd suggest running the tail through 'tr' to squeeze out the extra spaces first, and don't put the result back into the same file:

Code:
tail -$SomeNumber $file | tr -s " " | cut -d" " -f6 > ${file}.tmp;

Also, if there is a space at the beginning of the line, your field number will need to increase to '7'. For example, in the lines below:

Code:
field1 field2 field3

 field1 field2 field3

The first line, lines up as you'd expect with a cut using a space as a delimiter. But the second line will have a null field as the first field
making field1 line up with -f2 in cut and field2 line up with -f3, etc.

Last edited by rwuerth; 02-04-2009 at 09:58 AM..
# 3  
Old 02-04-2009
First off: I don't have a real problem I need to get fixed. And I know my way around on Linux fairly well.

Now: It's not a problem with 'cut' per se. The value I need is actually in the 4th column but since there are two spaces at the begining of ea. line i have to take -f6. No problems here and that's not what was puzzling me!

The thing where my script goes wrong is the redirection after the cut operation. Writing to the same file which was used in the 'tail' doesn't work. Add some arbitrary extension to $file (like you suggested and I already posted) and everything works fine.

I was just wondering where the problem is exactly.
Considering the following flow of command execution:
- tail opens the file, operates on it, closes it (in that order? I don't know)
- the pipe runs the stdout to cut (does this create a subshell???)
- cut takes some column (which contains data and is not empty)
- output redirection to file fails and the resulting files are empty.
BUT: Sometimes (about every 100 times) it does what is expected and the file only contains field 6!

I also tried to debug that line by investigating the output of every command involved. There is no problem except when the last output is redirected to the file.

Again: I don't need help in getting the script to work! Thanks for trying though! Smilie

Edit: Ok I tried s.th. here: Instead of redirecting the output to a file with > I did it with 'tee'. After the tee the output is sent to /dev/null. Result: No erronous behaviour as above. Shell script is faster by 2.5 secs (corresponds to 30% in my case). Nice!

Last edited by BandGap; 02-04-2009 at 10:49 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Script execution is very slow when trying to find all files and their owners on HP-UX box

Hi, I have a HP-UX server were I need to list all the files in the entire file system, their directory path, last modified date, owner and group. I do not need to search the file contents. I created the script given below and I am excluding directories and files of type tmp, temp and log. The... (4 Replies)
Discussion started by: Adyan Faruqi
4 Replies

2. Shell Programming and Scripting

Command very Slow

Hi, The below command takes more than 2hrs. result=$(find . -name "star_st*" -exec head -1 {} \; | grep "1175 876330") Is there a way I can tweak the command to get better performance / quicker results. Suggestions would be appreciated. (20 Replies)
Discussion started by: mohtashims
20 Replies

3. Shell Programming and Scripting

Multiple command execution inside awk command during xml parsing

below is the output xml string from some other command and i will be parsing it using awk cat /tmp/alerts.xml <Alert id="10102" name="APP-DS-ds_ha-140018-componentFailure-S" alertDefinitionId="13982" resourceId="11427" ctime="1359453507621" fixed="false" reason="If Event/Log Level(ANY) and... (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

Want to terminate command execution when string found in the command output

Hi Experts, I am very much new to linux scripting, I am currently working on reducing my manual work and hence writing a script to automate few task. I am running below command to snmpwalk the router.. snmpwalk -v 3 -u WANDL_SU -a MD5 -A vfipmpls -x DES -X VfIpMpLs -l authPriv... (19 Replies)
Discussion started by: Hanumant.madane
19 Replies

5. Shell Programming and Scripting

Making a faster alternative to a slow awk command

Hi, I have a large number of input files with two columns of numbers. For example: 83 1453 99 3255 99 8482 99 7372 83 175 I only wish to retain lines where the numbers fullfil two requirements. E.g: =83 1000<=<=2000 To do this I use the following... (10 Replies)
Discussion started by: s052866
10 Replies

6. Shell Programming and Scripting

Slow Script Execution.

Basically my requirement is to know the total number of free anonymous ports. anonymous port range is 32768- 65535. i wrote a script for that ********************************************** for i in {32768..65535} do netstat -an | grep $i > /dev/null if ... (21 Replies)
Discussion started by: mohtashims
21 Replies

7. Solaris

Slow while running a command for the first time

I am facing a performance problem on a Solaris 10 Sparc V890 server, it is an old one I know. The first time we realized there is a problem with the server, is the time when ftp transfers are made. There were 4 other identical servers doing much better. Network drivers are checked and there... (3 Replies)
Discussion started by: royalliege
3 Replies

8. Shell Programming and Scripting

Code for execution command by command

a) cd /root/user/dir/sd/disk ./runInstaller -silent -responseFile b) cd /root1/user1 cp /root/user/dir/sd/disk/ram.txt now a) executes and starts running but b) interupts a) and is executed while a) is running on the other hand so I want b) to start only after successfull completion of... (6 Replies)
Discussion started by: sriki32
6 Replies

9. Shell Programming and Scripting

File processing is very slow with cut command

Dear All, I am using the following script to find and replace the date format in a file. The field18 in the file has the following format: "01/26/2010 11:55:14 GMT+04:00" which I want to convert into the following format "20100126115514" for this purpose I am using the following lines of codes:... (5 Replies)
Discussion started by: bilalghazi
5 Replies

10. UNIX for Advanced & Expert Users

command execution ??

hi i have small shell script as follows cd /utilities/promoter/tmp grep SENDREPLY $1 | grep 'zzzzz@zzz.com' | awk -F" -f1 > /tmp/$LOGNAME/$1.request cd /tmp/$LOGNAME grep -e "\.sql" -e "\.md" $1.request > upd_$1.txt grep -v -e "\.sql" -e "\.md" $1.request > copy_$1.txt ... (1 Reply)
Discussion started by: zedex
1 Replies
Login or Register to Ask a Question