Delete a section of a file if...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete a section of a file if...
# 1  
Old 05-09-2012
Delete a section of a file if...

i have a file as below that has n section [here there is 2 section]:
[in_file]
Code:
 2006 0101 1236 49.3 L  37.902  48.482  0.0  Teh  5 0.2 2.7LTeh                1 
 GAP=238                                                                       E 
 Iranian Seismological Center, Institute of Geophysics, University of Tehran   6 
 STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO SNR AR TRES  W DIS CAZ7 
 HSH  SZ E    0   1237 29.15      2843.4 0.04                           126 238 
 AZR  SZ E    0   1237 58.33       598.9 0.14                           221 264 
 MRD  SZ E    0   1238  5.28       615.6 0.22                           258 291 
 BST  SZ E    0   1237 31.34       897.3 0.08                           143 261 
 SHB  SZ E    0   1238  2.03       222.2 0.16                           254 279 
 AZR  SE EPn  0   1236 40.89                                     0.0    0.0   0 
 BST  SN EPn  0   1236 45.64                                     0.0    0.0   0 
 HSH  SZ EPg  0   1237 9.75                                     -0.0    126 238 
 BST  SZ EPn  4   1237 10.80                                    -3.6    143 261 
 AZR  SZ EPn  0   1237 24.31                                     0.3    221 264 
 SHB  SZ EPn  0   1237 27.99                                     0.0    254 279 
 MRD  SZ EPn  0   1237 28.80                                     0.3    258 291 
 BST  SN ELg  0   1237 29.38                                    -0.0    143 261 
 AZR  SE ELg  0   1237 51.43                                     0.0    221 264 
 
 2006 0101 1316 22.9 L  34.933  46.292  2.0  Teh  4 0.3 1.2LTeh                1 
 GAP=290                                                                       E 
 Iranian Seismological Center, Institute of Geophysics, University of Tehran   6 
 STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO SNR AR TRES  W DIS CAZ7 
 GHG  SZ E    0   1316 44.78      2607.6 0.06                          72.0 159 
 LIN  SZ E    0   1316 42.60      2426.4 0.08                          61.0  91 
 DHR  SZ E    0   1316 30.90    108786.0 0.04                          27.0 161 
 KOM  SZ E    0   1317  7.20       713.4 0.18                           140 126 
 DHR  SZ EPg  0   1316 27.32                                    -0.1   27.0 161 
 DHR  SN ESg  0   1316 30.52                                     0.0   27.0 161 
 LIN  SZ EPg  0   1316 32.78                                    -0.1   61.0  91 
 GHG  SZ EPg  0   1316 34.96                                     0.4   72.0 159 
 LIN  SN ESg  0   1316 40.32                                     0.0   61.0  91 
 KOM  SZ EPg  0   1316 45.20                                    -0.4    140 126

i want if the number value of 12th field (i bold it in the in_file) from first line of each section be less than 2, that section be deleted.{each section separate with a with space}.
so the result must be:
[desired]:
Code:
 2006 0101 1236 49.3 L  37.902  48.482  0.0  Teh  5 0.2 2.7LTeh                1 
 GAP=238                                                                       E 
 Iranian Seismological Center, Institute of Geophysics, University of Tehran   6 
 STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO SNR AR TRES  W DIS CAZ7 
 HSH  SZ E    0   1237 29.15      2843.4 0.04                           126 238 
 AZR  SZ E    0   1237 58.33       598.9 0.14                           221 264 
 MRD  SZ E    0   1238  5.28       615.6 0.22                           258 291 
 BST  SZ E    0   1237 31.34       897.3 0.08                           143 261 
 SHB  SZ E    0   1238  2.03       222.2 0.16                           254 279 
 AZR  SE EPn  0   1236 40.89                                     0.0    0.0   0 
 BST  SN EPn  0   1236 45.64                                     0.0    0.0   0 
 HSH  SZ EPg  0   1237 9.75                                     -0.0    126 238 
 BST  SZ EPn  4   1237 10.80                                    -3.6    143 261 
 AZR  SZ EPn  0   1237 24.31                                     0.3    221 264 
 SHB  SZ EPn  0   1237 27.99                                     0.0    254 279 
 MRD  SZ EPn  0   1237 28.80                                     0.3    258 291 
 BST  SN ELg  0   1237 29.38                                    -0.0    143 261 
 AZR  SE ELg  0   1237 51.43                                     0.0    221 264


Last edited by oreka18; 05-09-2012 at 10:15 AM..
# 2  
Old 05-09-2012
Code:
# awk '$1 == "2006" && $12 > 2 {i++} /^[ \t]*$/ {i--} i' infile
2006 0101 1236 49.3 L  37.902  48.482  0.0  Teh  5 0.2 2.7LTeh                1
 GAP=238                                                                       E
 Iranian Seismological Center, Institute of Geophysics, University of Tehran   6
 STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO SNR AR TRES  W DIS CAZ7
 HSH  SZ E    0   1237 29.15      2843.4 0.04                           126 238
 AZR  SZ E    0   1237 58.33       598.9 0.14                           221 264
 MRD  SZ E    0   1238  5.28       615.6 0.22                           258 291
 BST  SZ E    0   1237 31.34       897.3 0.08                           143 261
 SHB  SZ E    0   1238  2.03       222.2 0.16                           254 279
 AZR  SE EPn  0   1236 40.89                                     0.0    0.0   0
 BST  SN EPn  0   1236 45.64                                     0.0    0.0   0
 HSH  SZ EPg  0   1237 9.75                                     -0.0    126 238
 BST  SZ EPn  4   1237 10.80                                    -3.6    143 261
 AZR  SZ EPn  0   1237 24.31                                     0.3    221 264
 SHB  SZ EPn  0   1237 27.99                                     0.0    254 279
 MRD  SZ EPn  0   1237 28.80                                     0.3    258 291
 BST  SN ELg  0   1237 29.38                                    -0.0    143 261
 AZR  SE ELg  0   1237 51.43                                     0.0    221 264

This User Gave Thanks to zaxxon For This Post:
# 3  
Old 05-09-2012
it doesn't work when i test it the out put file is empty! please test youe script one more time on the original file :<[in_file]>
# 4  
Old 05-09-2012
It works absolutely fine.

If it doesn't work for you, try nawk.
# 5  
Old 05-10-2012
take a look at 1st field of 1st line of your test [2006], it's one space to the left and this is the cause that shows your script works! change it and see that it doesn't work!
# 6  
Old 05-10-2012
Listen up oreka18. I did copy and paste to your given example and still have it on my disk. You changed your example afterwards to accuse me to have done an error?
And the best thing is, there is no problem with this for the code I have given. It has no problem if there is a leading blank or not. You don't even understand the code the slightest way and don't even try.
Anyway - maybe try to be more sneaky next time - you get a ban for this. Try some other forum to get your work done for you. We have a good duplicity detection system so if you try to register again, you just get another ban.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to change file section into each line?

Hi Gurus, I have below file which has different sections, need to move the sections to beginning of the each record. original file aaa bbb ccc ddd eee fff output file. aaa bbb ccc ddd eee fff (6 Replies)
Discussion started by: green_k
6 Replies

2. Shell Programming and Scripting

How to segregate a section from big file?

Hello, I need to know all IP range (ip_prefix), associated with us-west-2 region only from this link - https://ip-ranges.amazonaws.com/ip-ranges.json (it can be opened in wordpad for better visibility) Please suggest, how would I do it. If vi, awk or sed is needed, I have downloaded it on my... (7 Replies)
Discussion started by: solaris_1977
7 Replies

3. Shell Programming and Scripting

awk to lookup section of file in a range of another file

In the below, I am trying to lookup $1 and $2 from file1, in a range search using $1 $2 $3 of file2. If the search key from file1 is found in file2, then the word low is printed in the last field of that line in the updated file1. Only the last section of file1 needs to be searched, but I am not... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. Shell Programming and Scripting

Extract record from file based on section.

input file output file (1 Reply)
Discussion started by: lathigara
1 Replies

5. Shell Programming and Scripting

Fetch a section from a file

Hi, I have a file like... $cat file1 +++++++++++++++++++ client1 +++++++++++++++++++++++++++++ col1 col2 col3 ------ ----- ----- (0 rows affected) ========================================================= +++++++++++++++++++ client1 +++++++++++++++++++++++++++++ col1 col2 col3... (6 Replies)
Discussion started by: sam05121988
6 Replies

6. Shell Programming and Scripting

Read section of file and take decision

Hi, My requirement is that I've to read a config file having below info Oracle number|double;integer -> user can put multiple values here blob|binary varchar2|string varchar|string number(p,s)|decimal timestamp|date/time and so on .... Sybase bigint|bigint datetime|date/time... (2 Replies)
Discussion started by: dips_ag
2 Replies

7. Shell Programming and Scripting

Extract section of file based on word in section

I have a list of Servers in no particular order as follows: virtualMachines="IIBSBS IIBVICDMS01 IIBVICMA01"And I am generating some output from a pre-existing script that gives me the following (this is a sample output selection). 9/17/2010 8:00:05 PM: Normal backup using VDRBACKUPS... (2 Replies)
Discussion started by: jelloir
2 Replies

8. Shell Programming and Scripting

cutting a section of a big file

Hi, I have a text file 10giga size. Opening the file with vi takes forever ... Im intersting only with the 100 first records. Is there way to copy those 100 lines to new file (with no need to open the file)? Thanks (6 Replies)
Discussion started by: yoavbe
6 Replies

9. Shell Programming and Scripting

Using Sed to duplicate a section of a file....

hello all, I have a file like this: section 1 blah1 blah2 section 2 blah1 blah2 section 3 blah1 blah2 and I want to use sed to duplicate section 2, like this: section 1 blah1 blah2 section 2 blah1 blah2 section 2 blah1 (2 Replies)
Discussion started by: nick26
2 Replies

10. UNIX for Dummies Questions & Answers

help find a section line of a file

hi, I have a 20 line file. I need a command which will brinf back a specific line based upon the line number I enter. e.g. the file looks like this and is called file1 jim is a man john is a woman james is a man wendy is a woman lesley is a woman i want a command that will... (4 Replies)
Discussion started by: sureshy
4 Replies
Login or Register to Ask a Question