search one row and print next 9 rows


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers search one row and print next 9 rows
# 1  
Old 07-07-2010
search one row and print next 9 rows

Hi,

I'd like your support on this, my file looks like this:

Code:
25 50 200 10 0 1
10 6 12 17 26 24 30 319 10 -0.83 3.31 270.646 12.48
-163.04 30.38 0 0 0 -2.88 0.64 16.06 34.4 33.08 34.4 33.08
34.06 20.48 6.22 -33.39 33.96
-33.5840381 -71.6177759 -163.26 25.59 34.1
-28.8 -5.6 -9.9 0 0 0 0 2.2 26.2
200 cm GGA dB 0.45 0.077
2.2 12.09 84.69 12 1.1 -2.5 -3.6 69.5 71.1 73.3 78.4 30 -0.08
3.2 13.54 68.95 12.6 4.9 -1.3 12.6 71.7 72.3 77.2 77.5 80 -0.81
4.2 18.47 91.96 18.5 -0.6 -3 13.8 73 73.5 76.7 77.5 80 0.71
5.2 10.53 330.77 -5.1 9.2 2.1 2 71.2 73.6 72.5 75.8 80 0.2
6.2 14.93 13.13 3.4 14.5 3.2 -0.2 74.2 73.4 74.7 74 90 0.4
7.2 27.97 14.42 7 27.1 5.3 6.2 74.5 75.6 74 74.5 90 0.14
8.2 10.09 357.77 -0.4 10.1 4.4 6.9 73.8 74.2 73.9 73.7 90 -0.48
9.2 17.73 36.61 10.6 14.2 -0.3 -5.8 73.5 73.4 72.5 76.6 80 -1.37
10.2 23.17 14.64 5.9 22.4 6.2 3.2 74.9 72.3 75.2 76.5 90 -1.81
11.2 17.77 46.43 12.9 12.2 4.7 -8.4 70.1 75.2 77 73.1 70 -0.08
12.2 6.85 72.92 6.6 2 9.8 3.7 69.1 78.4 79.3 72.6 70 0.4
13.2 8.84 274.45 -8.8 0.7 5.9 -3.4 69.4 78.1 78.9 71.5 90 -0.36
14.2 5.11 46.7 3.7 3.5 5.6 10.7 68.3 76.6 75.7 70.7 90 -0.37
15.2 10.09 78.34 9.9 2 3.7 6.5 69 73.8 74.4 71.3 100 -0.65
16.2 1.33 126.66 1.1 -0.8 9.4 -5.3 71.1 73 75.6 73.6 90 0.19
17.2 10.83 195.46 -2.9 -10.4 8.1 -3.2 69.8 73.6 76.1 74.1 90 0.18
18.2 11.02 21.98 4.1 10.2 0.6 -12.4 68.4 72.4 75.5 75.6 80 0.1
19.2 3.99 216.43 -2.4 -3.2 0.6 1.7 69.2 72.8 73.9 75.9 80 1.5
20.2 21 198.44 -6.6 -19.9 1.6 5.2 70.1 73.7 72.4 76.7 80 1.28
21.2 0.28 272.37 -0.3 0 -4.9 2.4 69.7 72.1 72.6 77.1 80 0.82
22.2 9.13 31.22 4.7 7.8 3 -6.3 69 72.9 72.5 78.2 90 0.51
23.2 18.67 123.04 15.6 -10.2 13.5 13.5 69.2 75.2 72.4 79.2 60 0.17
24.2 12 230.6 -9.3 -7.6 8.5 -1.5 69.5 76.2 73.3 79.4 80 0.17
25.2 10.46 162.83 3.1 -10 10 -3.7 68.7 73.6 74.2 77.9 80 2.83
26.2 10.39 100.51 10.2 -1.9 4 20.5 68.9 73.5 76 77.1 70 1.1
27.2 19.43 191.21 -3.8 -19.1 33.9 15 68.8 76.1 76.9 77.9 30 2147483647
28.2 18.52 183.91 -1.3 -18.5 21.7 0.4 70.4 77.2 79.4 76.5 40 2147483647
29.2 14.28 293.12 -13.1 5.6 19.1 -6.5 73.1 79.5 78.5 75.6 40 2147483647
30.2 -32768 -32768 -32768 -32768 -32768 -32768 76.2 91.6 81.1 81.6 0 2147483647
31.2 -32768 -32768 -32768 -32768 -32768 -32768 84 108.7 88.8 96.8 0 2147483647
32.2 -32768 -32768 -32768 -32768 -32768 -32768 98.2 113.2 102.9 115.2 0 2147483647
33.2 -32768 -32768 -32768 -32768 -32768 -32768 110.7 255 109.6 122 0 2147483647
34.2 -32768 -32768 -32768 -32768 -32768 -32768 255 255 255 255 0 2147483647
35.2 -32768 -32768 -32768 -32768 -32768 -32768 255 255 255 255 0 2147483647
36.2 -32768 -32768 -32768 -32768 -32768 -32768 255 255 255 255 0 2147483647
37.2 -32768 -32768 -32768 -32768 -32768 -32768 255 255 255 255 0 2147483647
38.2 -32768 -32768 -32768 -32768 -32768 -32768 255 255 255 255 0 2147483647
39.2 -32768 -32768 -32768 -32768 -32768 -32768 255 255 255 255 0 2147483647
40.2 -32768 -32768 -32768 -32768 -32768 -32768 255 255 255 255 0 2147483647
...

I need to search the row with the pattern -33.5840381 -71.6177759 and then avoid the next two rows, and then print the next 9 rows (in read above). To find the pattern I tried:
Code:
grep '[-][0-9]\{2\}.[0-9]\{7\}' file.txt

but it just print the lines with that pattern. Two avoid the two rows I tried:

Code:
awk 'NR==6 || NR==7 {print}' file.txt

but I know the current position of them in the example (this is one second of my data). However, my file is 30000 rows with a similar structure.

To print the next 9 rows, I tried this:
Code:
awk 'NR>4 && NR<17' file.txt

but like above, I know the current position of them.

I'd greatly appreciate your support.

Regards.

Last edited by vgersh99; 07-07-2010 at 01:46 PM.. Reason: code tags, please!
# 2  
Old 07-07-2010
something to start with:
Code:
nawk '$1=="-33.5840381" && $2=="-71.6177759" {c=11;next} c&&--c<9' myFile

# 3  
Old 07-07-2010
Old school (and probably very fast):

Place the following into a file called test.sed:

Code:
/^-33.5840381 -71.6177759/ b a
b
:a
n
n
n
p
n
p
n
p
n
p
n
p
n
p
n
p
n
p
n
p
q

Then do:

sed -n -f test.sed file.txt
# 4  
Old 07-07-2010
Thank you for the answers, based on the former code I did the following:

Code:
adcp=adcp.txt
latlong=latlong.txt

awk '{ print $1, $2 }' $adcp | grep '[-][0-9]\{2\}.[0-9]\{7\}' > $latlong
for ((row=1;row<55;row++))
do
        lat=`awk 'NR=='$row'' $latlong | awk '{ print $1}'`
        long=`awk 'NR=='$row'' $latlong | awk '{ print $2}'`
        echo "$lat $long"
        awk '$1=="'$lat'" && $2=="'$long'" {c=11;next} c&&--c<9' $adcp
done

and it works apparently as I want, I have to check it. However, this is the result I was looking for at the beginning:

Code:
-33.5840381 -71.6177759
2.2     12.09   84.69   12      1.1     -2.5    -3.6    69.5    71.1    73.3    78.4    30      -0.08
3.2     13.54   68.95   12.6    4.9     -1.3    12.6    71.7    72.3    77.2    77.5    80      -0.81
4.2     18.47   91.96   18.5    -0.6    -3      13.8    73      73.5    76.7    77.5    80      0.71
5.2     10.53   330.77  -5.1    9.2     2.1     2       71.2    73.6    72.5    75.8    80      0.2
6.2     14.93   13.13   3.4     14.5    3.2     -0.2    74.2    73.4    74.7    74      90      0.4
7.2     27.97   14.42   7       27.1    5.3     6.2     74.5    75.6    74      74.5    90      0.14
8.2     10.09   357.77  -0.4    10.1    4.4     6.9     73.8    74.2    73.9    73.7    90      -0.48
9.2     17.73   36.61   10.6    14.2    -0.3    -5.8    73.5    73.4    72.5    76.6    80      -1.37
10.2    23.17   14.64   5.9     22.4    6.2     3.2     74.9    72.3    75.2    76.5    90      -1.81
-33.5840694 -71.6181737
2.2     21.15   136.15  14.7    -15.3   -4      2       72.9    73.2    76.4    78.2    40      2.22
3.2     14.31   180.2   0       -14.3   -1.7    1.3     74.3    72.3    77.3    80.1    70      -0.09
4.2     23.48   148.64  12.2    -20.1   1.8     4       76.1    74.5    77.3    79.2    90      0.53
5.2     15.51   214.33  -8.7    -12.8   2.9     -1.8    74.4    79.5    75.9    78.1    90      1.16
6.2     11.83   128.48  9.3     -7.4    1.9     -6.3    75.6    78.6    74.9    74.9    80      0.29
7.2     18.88   129.58  14.6    -12     -1.3    4.8     76.3    75.3    75.8    74.2    80      0.91
8.2     30.63   167.46  6.7     -29.9   3.8     6.2     75.8    72      76.1    75.7    90      0.9
9.2     19.4    176.61  1.1     -19.4   6       -6.4    75.8    73.3    74.3    74.4    90      0.7
10.2    21.29   142.15  13.1    -16.8   3.8     -5.6    74.3    74.2    75.5    73.8    90      1.86
-33.5840887 -71.6185953
2.2     27.73   166.63  6.4     -27     3.8     -6.1    72.3    68.3    72.6    74.9    70      3.11
3.2     2.17    67.74   2       0.8     1.7     -5.1    78.4    73.4    78.4    75.8    90      -0.33
4.2     7.07    132.53  5.2     -4.8    0.4     1.7     79.7    74      75.3    75.6    70      0.9
5.2     12.89   210.31  -6.5    -11.1   4.3     6.9     75      75.6    73.9    78.9    90      0.15
6.2     6.9     250.16  -6.5    -2.3    2.3     -14.9   77.8    74.1    74.5    78.3    70      0.25
7.2     5.02    185.91  -0.5    -5      3.7     0.5     79      75      77.1    74.3    80      0.29
8.2     7.67    265.91  -7.7    -0.5    0       9.8     76.3    76      77.9    75.4    70      0.97
9.2     3.98    160.49  1.3     -3.8    3.4     1.4     74.8    76.9    74.8    77.1    100     1.16
10.2    20.67   99.21   20.4    -3.3    3.6     3.7     74      77.2    75.5    76.2    80      0.9
-33.5840663 -71.6190712
2.2     10.02   158.6   3.7     -9.3    4.8     -3.6    69.9    71.1    75.9    75.8    30      2147483647
3.2     2.85    213.7   -1.6    -2.4    0       5.3     73      71.9    74.8    74.1    100     0.44
4.2     8.62    127.52  6.8     -5.2    0.7     -4.8    71.1    72.6    81.3    77.6    80      -0.46
5.2     3.68    2.37    0.2     3.7     1.9     -1.5    74.6    72.8    79.9    78      70      0.23
6.2     3.56    305.73  -2.9    2.1     1.4     -7.4    73.5    75.7    80.9    77.6    80      0.62
7.2     8.3     337.14  -3.2    7.7     -3.3    -6.5    73.7    75      80.3    77      70      0.72
8.2     9.01    58      7.6     4.8     -0.6    1.9     73.6    74      79      75.5    70      0.47
9.2     4.5     69.12   4.2     1.6     4.8     -17     73      75.9    77.6    76.1    80      -0.43
10.2    18.49   82.15   18.3    2.5     4.1     -20.6   72.8    76.8    77.2    77      90      -0.46
-33.5840623 -71.619554
...

Thanks so much, I'll check and let you know if it's what I want.

---------- Post updated at 04:09 PM ---------- Previous update was at 03:28 PM ----------

Yes guys, the code I recently used works as I want, thank so much again.

Last edited by vgersh99; 07-07-2010 at 04:30 PM.. Reason: fixed code tags
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

2. Shell Programming and Scripting

Print row on 4th column to all row

Dear All, I have input : SEG901 5173 9005 5740 SEG902 5227 5284 SEG903 5284 5346 SEG904 5346 9010 SEG905 5400 5456 SEG906 5456 5511 SEG907 5511 9011 SEG908 5572 9015 SEG909 5622 9020 SEG910 5678 5739 SEG911 5739 5796 SEG912 5796 9025 ... (3 Replies)
Discussion started by: attila
3 Replies

3. Shell Programming and Scripting

Search row by row from one file to another file if match is found print few colums of file 2

this is the requirement list.txt table1 table2 table3 testfile.txt name#place#data#select * from table1 name2#place2#data2#select * from table 10 innerjoin table3 name2#place2#data2#select * from table 10 output name place table1 name2 place table3 i tried using awk (7 Replies)
Discussion started by: vamsekumar
7 Replies

4. Shell Programming and Scripting

Get row number from file1 and print that row of file2

Hi. How can we print those rows of file2 which are mentioned in file1. first character of file1 is a row number.. for eg file1 1:abc 3:ghi 6:pqr file2 a abc b def c ghi d jkl e mno f pqr ... (6 Replies)
Discussion started by: Abhiraj Singh
6 Replies

5. Shell Programming and Scripting

Print various rows in one row

I have this in a file 11.22.33.44 yyyyyyuser With awk/sed, I need this to be output as follows alias server.domain.com='ssh yyyyyyuser@11.22.33.44' (4 Replies)
Discussion started by: anil510
4 Replies

6. Shell Programming and Scripting

complicated row to rows conversion

I have a file containing rows with the following format. Field1|Field2|Field3|data1:data data2:data data3:"dataA:data dataB:data" data4:data:data (and so on) I need to format the above row into multiple rows that look like this: Field1|Field2|Field3|data1|data ... (2 Replies)
Discussion started by: newreverie
2 Replies

7. UNIX for Advanced & Expert Users

Converting rows to a single row

Hi all I have a file as below : Development System User Production i want to convert the file to below format: "Development","System","User","Production" Is it possible with UNIX ? if so can you please give me some direction on it ? Thanks, Satya Use code tags please, ty. (10 Replies)
Discussion started by: satyaranjon
10 Replies

8. UNIX for Advanced & Expert Users

convert rows to single row

Hi I want to convert multiple rows ro single row ,I have tried with below one but I am not getting what I am expecting.Please any idea a.txt conn1=stg conn2=dev path=\xxx\a1.txt fre=a conn1=stg conn2=dev path=\xxx\a2.txt freq=a awk '/a/{ORS=" "}{print}END{print "\n"}'... (5 Replies)
Discussion started by: akil
5 Replies

9. Shell Programming and Scripting

Insert rows with computations of next row

Hello folks, I have data collected in every 3 hours. But, I would like to expand this to 1 hour interval by equally dividing with next row. For example, I want to keep the first value 1987-01-01-00z 2.0, but following all record should be re-written as follow. 1987-01-01-03z 5.0 becomes... (11 Replies)
Discussion started by: Jae
11 Replies

10. Shell Programming and Scripting

Concatenate 2 rows into 1 row

I need to search a file for two values (valueA & valueB). ValueA will be on a different row than valueB, and concatenate the two together on the same row of my output. Example: search input file for strings "node" and "OS", combine the two results into one row input node A text text OS... (4 Replies)
Discussion started by: indianadoug
4 Replies
Login or Register to Ask a Question