![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Results of command execution into array | alirezan | Shell Programming and Scripting | 2 | 08-18-2008 11:17 PM |
| Execution of awk command in a variable | patelamit009 | Shell Programming and Scripting | 2 | 08-07-2008 11:04 AM |
| command execution time | hashin_p | Shell Programming and Scripting | 5 | 07-06-2008 07:28 PM |
| command execution ?? | zedex | UNIX for Advanced & Expert Users | 1 | 11-06-2007 05:26 PM |
| Status code of last command execution | tipsy | Shell Programming and Scripting | 1 | 07-21-2006 01:14 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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; 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.. |
|
||||
|
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;
Code:
field1 field2 field3 field1 field2 field3 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.. |
|
||||
|
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! ![]() 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.. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|