Avoiding For Loop in Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Avoiding For Loop in Shell Script
# 1  
Old 08-18-2011
Avoiding For Loop in Shell Script

I am looking to a solution to the following problem. I have a very large file that looks something like this:

Quote:
0.111 0.111 0.788 0.101 0.800 0.099 0.500 0.255 0.245
0.234 0.675 0.091 0.100 0.088 0.812 0 0 1
Each group of three numbers on each line are three probabilities that sum to one.

I want to output the maximum for each group of three. So desired output would be:

Quote:
0.788 0.800 0.500
0.675 0.812 1
or

Quote:
0.788
0.800
0.500
0.675
0.812
1
I've written the following kludge which works just fine on a small subset of this data but does not scale up well (because it involves an ugly triple nested for loop):

Code:
#!/bin/sh
length=$(awk '{n++}END{print n}' file )
width=$(awk 'NR == 1 { print NF }' file )
width=$( expr $width / 3 )
 
for ((i=1; i <= length; i++))
do
for ((j=1; j <= width; j++))
do
a=$(expr 1 + \( \( $j - 1 \) \* 3 \) )
b=$(expr $a + 2)
awk -v i=$i -v a=$a -v b=$b 'NR == i { for (k = a; k <= b; k++ )if ( $k > max) max = $k } END { print max}' file >> out
done
done

Does anyone know of a solution that will scale up well? Can it be accomplished with basic unix utilities/shell scripting?

Thanks in advance!
# 2  
Old 08-18-2011
It's not the ugly triply-nested for-loop that's making it slow, it's the running of multiple external processes per loop. It's wasteful to run awk, grep, sed, and so forth for individual lines -- they are efficient when run on batches of data, but take time to load and quit. Imagine being only allowed to say one word per telephone call... Or in your case, having to say 10,000 words per phone call but only one of them with any meaning.

You're also using externals like expr when your shell (probably) supports better ways of doing math. expr is inefficient for the reason listed above but some shells have nothing better.

How one could do this more efficiently depends on what utilities are available and what system you have. what are they?
These 2 Users Gave Thanks to Corona688 For This Post:
# 3  
Old 08-18-2011
Is this what you were after:

Code:
awk '
   { for(i=1;i<=NF;i++) {
      max=$i>max?$i:max;
      if(!(++cnt%3)) {
          print max;
          max=-999;
   }}}
   END { if(cnt%3) print max }' file

@Corona688 - One word per phone call - Nice analogy, I'll file that one away for later use

Last edited by Chubler_XL; 08-18-2011 at 10:31 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 08-18-2011
Another awk:
Code:
awk '                                                                        
function max (a, b) {    
  return a>b ? a : b;   
}                     
{ for (i=1; i<=NF; i+=3) {
    printf max( max($i, $(i+1)), $(i+2)) " "
  }   
  print ""                               
}
' INPUTFILE

This User Gave Thanks to yazu For This Post:
# 5  
Old 08-18-2011
Yet another:
Code:
tr -s ' \t' '\n\n' < file | awk '$0>max+0 {max=$0} !(NR%3) {print max; max=0}' > out


Test run:
Code:
$ cat data
0.111 0.111 0.788 0.101 0.800 0.099 0.500 0.255 0.245
0.234 0.675 0.091 0.100 0.088 0.812 0 0 1
$ tr -s ' \t' '\n\n' < data | awk '$0>max+0 {max=$0} !(NR%3) {print max; max=0}'
0.788
0.800
0.500
0.675
0.812
1


Regards,
Alister
This User Gave Thanks to alister For This Post:
# 6  
Old 08-18-2011
My solution will support lines with number of field not evenly divisible by 3 - handy if that situation can occur.

Code:
$ cat file
0.111 0.111 0.788 0.101 0.800 0.099 0.500 0.255 0.245 
0.234 0.675 0.091 0.100 0.088 0.812 0 0 1 7

# 7  
Old 08-19-2011
Thanks Everyone! I like that one word per phone call analogy! I now understand the problem much better.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Loop in shell script

Hi Friends, I have a file. the content is as below: file1: /A/B/C/abc.txt 2013-07-28 13:50:00,2013-07-31 01:00:00,5,710 /A/B/C/xyz.txt 2011-09-21 18:30:00,2013-07-30 06:15:00,15,65135 2009-11-09 18:00:00,2011-09-02 09:00:00,5,12345 2013-07-28 13:50:00,2013-07-31 01:00:00,5,710 ... (2 Replies)
Discussion started by: vsachan
2 Replies

2. Shell Programming and Scripting

Avoiding some files inside a loop

In my script I need to loop around some files like below example files are fa.info.abcd fa.info.bxde fa.info.cdas ------ for test_data in fa.info.* do # Some text appending logic applied # Copy to another directory done Now I need to discard some files while looping around ... (9 Replies)
Discussion started by: smile689
9 Replies

3. Shell Programming and Scripting

Help with the For loop shell script

Hi, I have multiple files in a directory. Each file will have a header.I have to check if any of the files has 0 rows other than the header then I have to delete the files. Here “ Empty file” in my case means a file has header information but no data. I have to delete such files. If the file... (2 Replies)
Discussion started by: ganesnar
2 Replies

4. UNIX for Dummies Questions & Answers

Avoiding the second run of the script

Hi all, I want to put a check in my script to check if the same instance is already running and not finished and if not then does not allow it to run! in which part of my script I should put this? and any idea how I should write it? tx (4 Replies)
Discussion started by: messi777
4 Replies

5. Shell Programming and Scripting

Help with IF statement with loop Shell Script

Hello I am very new to shell and I bought some books and trying to learn it. I started trying to write a script that will take a number and count it down to 1 with commas in between. This number can only be one argument. If lower than one or higher than one argument it sends an error message. ... (4 Replies)
Discussion started by: zero3ree
4 Replies

6. Shell Programming and Scripting

Shell script using loop

Hi everyone, I have n number of data in my file "temp" in following order.In each line table_name and column_name are different.input data is in same format each query in three lines. ALTER TABLE table_name ADD ( column_name1 VARCHAR2(10), column_name2 VARCHAR2(70) ); ... (23 Replies)
Discussion started by: alisha
23 Replies

7. Shell Programming and Scripting

Loop in shell script

Dear experts, i am quite new to shell script please any one can help me in this regard i would like write a script which takes input in the form >./Test.sh a,10,b,20,c,30... in this way i can give input in any number which is not constant in the end through loop i want to... (3 Replies)
Discussion started by: vin_pll
3 Replies

8. Shell Programming and Scripting

Help with loop in a shell script

I just want to write a little script, that reads the lines from a file, echos somthing in a new tmp.file and then do some commands whith the tmp.files. while read -r line do echo "TEST=" > tmp.$$ echo "$line" >> tmp.$$ any_command < tmp.$$ done < $INPUTFILE But I think I have to... (2 Replies)
Discussion started by: elifchen
2 Replies

9. Shell Programming and Scripting

Problem with while loop in shell script

Hi All, How to read a file upto last line(End Of Line) I wrote below program: cat R2_20060719.610.txt | while read LINE do echo "$LINE" done above code reading all lines from a file and skipping last line...... is there anything wrong in my code. Please help me out from this... (20 Replies)
Discussion started by: rkrgarlapati
20 Replies

10. Shell Programming and Scripting

If then else loop in Shell script

Hi Following is the code . When I give input as Bangalore,its dospalying Welcome to Hitech City. But say , if I select Delhi or US, its not displaying the corresponding message. Its still says Welcome to Hitech City. Seems that it not entering in the elif part. Please suggest. #!... (4 Replies)
Discussion started by: pankajkrmishra
4 Replies
Login or Register to Ask a Question