1

I am trying to use awk to manipulate data.I have a data file with two columns and I would like to find the row that contains an specific value. But there is more than one row that contains this value and I only would like to find the first one. I tried it with a for-loop and break but it does not work the way I expected.

{for (i=0;$2==100.0;min=$1){
        i++
        max=min+500
        if(i==1){
            print i"\t"min"\t"max
            break
            }
        }
}

After this loop there is a little bit more code which utilizes min and max.

Edit: The data looks like this

59.45   96
59.50   96
59.60   97
59.75   98
59.90   98
59.95   98
60.00   99
60.05   99
60.20   99
60.25   100
60.40   100
60.45   100
60.50   101
60.55   101
60.60   101
60.65   101
60.70   102
60.90   102
60.95   103
61.00   103
61.05   103
61.15   103
61.20   104
61.35   104
61.40   104
61.45   105
61.50   105
61.60   105
61.65   106
61.70   100
61.85   100

I would like to find the first row that contains 100in the second column and save the value from column one in a variable

  • 2
    What is the full command you had run? – cuonglm Aug 27 '14 at 8:26
  • 3
    Can you also provide some example dataset? – Paweł Rumian Aug 27 '14 at 8:29
  • @Gnouc: I am just saving the code above in a file named test.awk and run it with awk -f test.awk old.dat > new.dat – Froop Aug 27 '14 at 11:23
  • @Froop: So I think it failed because you never enter for loop. The condition $2==100.0 false. – cuonglm Aug 27 '14 at 11:29
1

You cannot use a for loop like that. Try something like:

awk '$2==100{print 1,$1,$1+500; exit}' OFS='\t' file
  • I tried this but I get an error message that break is not allowed outside of a loop. – Froop Aug 27 '14 at 11:32
  • @Froop Oww I meant exit of course. Changed it.. – Scrutinizer Aug 27 '14 at 11:40
0

Now I have a working solution

BEGIN {
    SearchMinFlag = 0;
    TempLimit = 195.0
    DeltaTime = 550
    TempMin = "LEER"
    FS = "\t"
}

# Stop MinSearch
NR > 2 && $1 > tstop {SearchMinFlag = 0}

# MinSearch
SearchMinFlag == 1 {
    ywert = $2;
    if (ywert < TempMin) {
        TempMin = ywert;
        TimeMin = $1
        }
}

# Start MinSearch
NR > 2 && $2 > TempLimit && SearchMinFlag == 0 && TempMin == "LEER" {
        SearchMinFlag = 1;
        tstart = $1
        tstop = tstart + DeltaTime
        ymerk = $2;
        TempMin = 10000
    }

# Result
END {
    print "t(T_min)", "T_min", "t_start", "t_stop\n"TimeMin, TempMin, tstart, tstop
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.