iterate through list of numbers and print specific lines with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting iterate through list of numbers and print specific lines with awk
# 1  
Old 09-17-2010
iterate through list of numbers and print specific lines with awk

Could someone please point me in the right direction with the following?

I have a program that generates logs that contains sections like this:

Code:
 IMAGE INPUT
     81   0  0.995  2449470    0   1726     368     1  0.0635  0.3291
     82   0  1.001  2448013    0   1666     365     1  0.0649  0.3235
     83   0  1.009  2444822    0   1697     371     1  0.0661  0.2888
     84   0  1.004  2447224    0   1733     362     1  0.0664  0.3346
     85   0  1.011  2446792    0   1704     357     1  0.0665  0.3024
     86   0  1.016  2450130    0   1675     362     1  0.0660  0.3208
     87   0  1.018  2448809    0   1719     349     2  0.0669  0.2914
     88   0  1.017  2448947    0   1710     357     2  0.0677  0.3044
     89   0  1.028  2447439    0   1721     368     2  0.0643  0.3164

  ACCEPTED

What I would like to do is produce an awk (or sed) one-liner that would
search between regular expression /IMAGE/ and /ACCEPTED and produce the following output:

Code:
 processing images 81 - 89

Where 81 and 89 are first fields of the first and last number-containing lines respectively. These numbers can be any range between "IMAGE" and "ACCEPTED", but are always consecutive and increment by 1.

Parsing first and last lines that only contain numbers in awk without multiple pipes has got me stuck.

Thanks!
# 2  
Old 09-17-2010
you can use the below command:--

Code:
awk '{print "processing images"$1}' ur_file

but this will give you all the numbers in 1 straight line....
# 3  
Old 09-17-2010
One way.
Code:
awk '/ACCEPTED/{print "processing images "x" - "y;exit}/IMAGE/{getline;x=$1}NF{y=$1}'  file


Last edited by danmero; 09-17-2010 at 09:18 AM.. Reason: Remove useless f
This User Gave Thanks to danmero For This Post:
# 4  
Old 09-17-2010
Thanks danmero!

---------- Post updated at 12:25 PM ---------- Previous update was at 11:43 AM ----------

So, I've almost got my little script running, but seem stuck again at printing out averages of columns.

What I have in my script is this:

Code:
program | tee $log | awk '\
/ACCEPTED/ {
    print "processing images "x" - " y}
/IMAGE/ {
    f=1;getline;x=$1} NF {y=$1}
{tot += $7; ++n} 
END { printf "average count is %3d\n", tot/n}
'

And what I would like to have is:
Code:
 processing images 1 - 9 average count is 362

where 362 is the average in column 7, for that section of the log (see below in my original post).

What I get with my script is this:
Code:
processing images 1 - 9
processing images 10 - 18
processing images 19 - 27
processing images 28 - 36
processing images 37 - 44
processing images 45 - 52
processing images 53 - 60
processing images 61 - 68
processing images 69 - 76
processing images 77 - 84
The average count is 4087

Thanks in advance.


Quote:
Originally Posted by euval
Could someone please point me in the right direction with the following?

I have a program that generates logs that contains sections like this:

Code:
 IMAGE INPUT
     81   0  0.995  2449470    0   1726     368     1  0.0635  0.3291
     82   0  1.001  2448013    0   1666     365     1  0.0649  0.3235
     83   0  1.009  2444822    0   1697     371     1  0.0661  0.2888
     84   0  1.004  2447224    0   1733     362     1  0.0664  0.3346
     85   0  1.011  2446792    0   1704     357     1  0.0665  0.3024
     86   0  1.016  2450130    0   1675     362     1  0.0660  0.3208
     87   0  1.018  2448809    0   1719     349     2  0.0669  0.2914
     88   0  1.017  2448947    0   1710     357     2  0.0677  0.3044
     89   0  1.028  2447439    0   1721     368     2  0.0643  0.3164

  ACCEPTED

What I would like to do is produce an awk (or sed) one-liner that would
search between regular expression /IMAGE/ and /ACCEPTED and produce the following output:

Code:
 processing images 81 - 89

Where 81 and 89 are first fields of the first and last number-containing lines respectively. These numbers can be any range between "IMAGE" and "ACCEPTED", but are always consecutive and increment by 1.

Parsing first and last lines that only contain numbers in awk without multiple pipes has got me stuck.

Thanks!
# 5  
Old 09-17-2010
Code:
awk '/ACCEPTED/{print "processing images "x" - "y" Average is "int(tot/nb)}/IMAGE/{getline;tot=nb=0;x=$1}$7>0{tot+=$7;nb++}NF{y=$1}' file

This User Gave Thanks to Chirel For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. Shell Programming and Scripting

Awk- Indexing a list of numbers in file2 to print certain rows in file1

Hi Does anyone know of an efficient way to index a column of data in file2 to print the coresponding row in file1 which corresponds to the data in file2 AND 30 rows preceding and after the row in file1. For example suppose you have a list of numbers in file2 (single column) as follows:... (6 Replies)
Discussion started by: Geneanalyst
6 Replies

3. Shell Programming and Scripting

How to print with awk specific field different from specific character?

Hello, i need help with awk. I have this file: cat number DirB port 67 er_enc_out 0 er_bad_os 0 DirB port 71 er_enc_out 56 er_bad_os 0 DirB port 74 er_enc_out 0 er_bad_os 0 DirB port 75 ... (4 Replies)
Discussion started by: elilmal
4 Replies

4. Shell Programming and Scripting

Print lines with numbers only.

Is there other way then this only solution I have found to print lines with numbers only? file.txtone two three 3 four five 5tt 6gfjdfgdf 9 66 six 0 seven 7 eight 546546 gffdg445gfg fd644 (7 Replies)
Discussion started by: Jotne
7 Replies

5. UNIX for Dummies Questions & Answers

grep specific number from a list of numbers

Hello. I have 9060 files labelled File1 to File9060. They are in numerical order. When I grep a file eg. File90 it will show me all files that contain the pattern "File90", eg File901 or File9001. I can only get specific files for File1000 or higher. How can I resolve this problem? Is there a... (5 Replies)
Discussion started by: godzilla07
5 Replies

6. Shell Programming and Scripting

Print Specific lines when found specific character

Hello all, I have thousand file input like this: file1: $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$ | | | |$$ $$ UERT | TTYH | TAFE | FRFG |$$ $$______|______|________|______|$$ $$ | | | |$$ $$ 1 | DISK | TR1311 | 1 |$$ $$ 1 |... (4 Replies)
Discussion started by: attila
4 Replies

7. Shell Programming and Scripting

print lines containing only numbers

Hi boys, I have a txt file with a lot of lines. It have lines containing mostly only numbers but some of them contain numbers mixed with special characters and or letters or space.. its look like this: 271261621371 2727127f 27126|71372. ... Do you have any ideas of the easiest way to... (3 Replies)
Discussion started by: alekkz
3 Replies

8. Shell Programming and Scripting

Problems to print specific lines with awk and grep...HELP!

Hi all I have data like this: model: 1, misfit value: 0.74987 1 1.182 1.735 2.056 1.867 2 0.503 1.843 2.018 1.888 3 2.706 2.952 2.979 1.882 4 8.015 3.414 3.675 1.874 ... (1 Reply)
Discussion started by: fedora2011
1 Replies

9. Shell Programming and Scripting

print first few lines, then apply regex on a specific column to print results.

abc.dat tty cpu tin tout us sy wt id 0 0 7 3 19 71 extended device statistics r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device 0.0 133.2 0.0 682.9 0.0 1.0 0.0 7.2 0 79 c1t0d0 0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx I want to skip first 5 line... (4 Replies)
Discussion started by: kchinnam
4 Replies

10. Shell Programming and Scripting

How to print specific lines with awk

Hi! How can I print out a specific range of rows, like "cat file | awk NR==5,NR==9", but in the END-statement? I have a small awk-script that finds specific rows in a file and saves the line number in an array, like this: awk ' BEGIN { count=0} /ZZZZ/ { list=NR ... (10 Replies)
Discussion started by: Bugenhagen
10 Replies
Login or Register to Ask a Question