Checking a file using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking a file using awk
# 1  
Old 10-25-2010
Checking a file using awk

I have a file that looks like this, with the first number in each block within each SOURCE declaration being a distance.


Code:
%( PHASES
P
%)

%( SOURCES
(10,0.0)
(13,0.0)
(16,0.0)
(19,0.0)
(22,0.0)
(25,0.0)
(28,0.0)
(31,0.0)
(34,0.0)
(37,0.0)
(40,0.0)
(43,0.0)
(46,0.0)
(49,0.0)
(52,0.0)
(55,0.0)
(58,0.0)
(61,0.0)
(64,0.0)
(67,0.0)
(70,0.0)
%)

%< SOURCE 1
%( PHASE 1
10 0 0
13 5.24674 5.24674
16 10.0159 10.0159
19 14.0214 14.0214
22 17.1469 17.1469
25 19.6792 19.6792
28 21.8606 21.8606
31 23.7148 23.7148
34 25.22 25.22
37 26.9128 26.9128
40 28.5765 28.5765
43 29.9023 29.9023
46 31.2833 31.2833
49 31.4122 31.4122
52 32.2555 32.2555
55 33.0052 33.0052
58 33.6344 33.6344
61 34.6833 34.6833
64 36.1779 36.1779
67 37.0386 37.0386
70 37.8864 37.8864
%)
%>

%< SOURCE 2
%( PHASE 1
10 5.81277 5.81277
13 0 0
16 6.18733 6.18733
19 9.73556 9.73556
22 13.7475 13.7475
25 17.1796 17.1796
28 19.9672 19.9672
31 22.0341 22.0341
34 24.1281 24.1281
37 26.3416 26.3416
40 27.4202 27.4202
43 28.2533 28.2533
46 29.7284 29.7284
49 30.9583 30.9583
52 31.6479 31.6479
55 32.7837 32.7837
58 33.2839 33.2839
61 34.436 34.436
64 34.8899 34.8899
67 35.7432 35.7432
70 36.1008 36.1008
%)
%>

I want to check if the difference between the value having the second and third numbers zero and the first number appearing in each block is greater than some user defined value. If the value is exceeded I display an error or warning.

For example, in the block below I find the value with the last two numbers 0, which is 13, then take the difference between each number and 13 and check if it exceeds a value, 65 say.

Code:
10 5.81277 5.81277
13 0 0
16 6.18733 6.18733
19 9.73556 9.73556
22 13.7475 13.7475
25 17.1796 17.1796
28 19.9672 19.9672
31 22.0341 22.0341
34 24.1281 24.1281
37 26.3416 26.3416
40 27.4202 27.4202
43 28.2533 28.2533
46 29.7284 29.7284
49 30.9583 30.9583
52 31.6479 31.6479
55 32.7837 32.7837
58 33.2839 33.2839
61 34.436 34.436
64 34.8899 34.8899
67 35.7432 35.7432
70 36.1008 36.1008


I have coded something like this but does not seem to work

Code:
  function abs(val) {
      return val > 0 ? val : -val
  }

# Change Record Separator and Field Separator.
  BEGIN {
    RS = "%<"
    FS = "\n"
  }

# 4. Skip the first two fields (each field is a line between RS="%<")
  /SOURCE [0-9]/ {
    for (i = 3; i <= NF; i++) {
      if (length($i) > 2) { # Skip %) and %> at the end
        split($i, a, " ")   # Get the three separate numbers
        if ((a[2] == 0) && (a[3] == 0)) { # Last two numbers are zero
          print ""
          for (j = 3; j <= NF; j++) {
            if (length($j) > 2) { # Skip %) and %> at the end
              split($j, b, " ")
              srdist = abs(a[1] - b[1])
              if (srdist > srxmax){
                ORS = ""
                print "ERROR: FNR="FNR", dist("a[1]","b[1]")="srdist
                ORS = "\n"
              }
            }
          }
        }
      }
    }
  }



---------- Post updated at 04:58 AM ---------- Previous update was at 04:48 AM ----------




Solved it now. Code is ok. Had just changed srxmax to srmax in the calling sequence.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: File Checking Issues with 9 multiple file

Hi, I have 9 files which are generated dynamically & if there is a some condition which doesn't meet the criteria then file is not created or is of zero size. so further i am unable to consolidate the files based on following code 1 awk -F, -v ptime="201407" 'FNR==1... (3 Replies)
Discussion started by: siramitsharma
3 Replies

2. Shell Programming and Scripting

awk - checking last three characters

Hello, I am working with some very large files (upwards of 1M records). I have written code to parse out a lot of the data and am using awk rather than a built-in "while read LINE" for performance (I have tested both ways). That said, I now need to read each of these incoming lines, check the ninth... (2 Replies)
Discussion started by: dagamier
2 Replies

3. Shell Programming and Scripting

Checking postfix mail log with AWK

Hello, Few days ago I found a person checking his mail log by AWK. But unfortunately I forget to take the command from him. Today I got to need that command badly. Here is a reference. When I check from my mail log like this it will not show me the details. grep "from=<noreply@panix.com>"... (2 Replies)
Discussion started by: rinti
2 Replies

4. Shell Programming and Scripting

Checking existence of file using awk

Hi, I need to check whether a particular file exists ot not using awk. Can anyone help me please? For Example:script that i am using: awk '{filename =$NF; rc=(system("test -r filename")) print $rc;}' "$1" is not working. Here I am passing a text file as input whose last word contains a... (6 Replies)
Discussion started by: manish007
6 Replies

5. Shell Programming and Scripting

Checking conditions with AWK

Input File1 0BB2 2A11 Split,FriApr80625,1507_7RAID5 0BF6 2829 Synchronized,FriJan140653,1507_7RAID5 0BF6 282A Split,FriApr80625,1507_7RAID5 0C7C 199E Synchronized,FriJan140653,1507_7RAID5 0C7C 1BCC Split,FriApr80625,1507_7RAID5 0DCA 0A9B ... (12 Replies)
Discussion started by: greycells
12 Replies

6. Shell Programming and Scripting

Awk and checking options

My task is that when the user calls the script 1. If user calls script with awk -v dtmax= -v stdlim= -f ../Scripts/add-rgauss-xt.awk fin.xt > fout.xt rgauss will return mean + (stdlim * sigma) 2. If user calls script with awk -v dtmax= -f ../Scripts/add-rgauss-xt.awk fin.xt > fout.xt... (4 Replies)
Discussion started by: kristinu
4 Replies

7. Shell Programming and Scripting

awk if percent % checking

hi everyone, # cat a a 10% b 25.5% c 91% d 50% # cat a | awk '$2 >= 90%; END {print $_}' awk: $2 > 90%; END {print $_} awk: ^ syntax error awk: each rule must have a pattern or an action part how to do only print when 2nd coln >= 90%. Thanks (6 Replies)
Discussion started by: jimmy_y
6 Replies

8. Shell Programming and Scripting

awk columnwise adjacent match checking program

Hi, For the below problem, awk script is needed.. input file : jill jack jill jill jack jill jack jill Required: here, i need to check from first line. reference is jack. jill need to verified with next line jack. if found print jill & jack combination hill1 jack & jill &... (0 Replies)
Discussion started by: vasanth.vadalur
0 Replies

9. Shell Programming and Scripting

Checking the length using awk??

I have following data in a file a.txt HELLO123456789 HELLO098765432 HELLO322366565 HELLO2343435 HELLO45343 I have to filter those lines whose length is not equal to 14 using awk. Thanks in advance:b: (1 Reply)
Discussion started by: nohup
1 Replies

10. Shell Programming and Scripting

Awk type checking

Hello, How to check if two variables( fields in awk) have the same datatype ( INTEGER , REAL, TEXT ) ? (2 Replies)
Discussion started by: scotty_123
2 Replies
Login or Register to Ask a Question