split continues lines to separated section with conditions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting split continues lines to separated section with conditions
# 1  
Old 10-14-2012
split continues lines to separated section with conditions

Hello;
i have a file contains N continues records. i want to split these lines to some separate sections with each lines of a section has the desired condition compared to other sections
input:
Code:
AZR ? ? ? Pn 37.202 48.82 1136119044 1136119009
SHB ? ? ?   Pn 37.802 48.02 1136119047 1136119008
MRD ? ? ? Pn 37.102 48.42 1136119048 1136119005 
DHR ? ? ? Pg 34.933 46.902 1136121387 1136121382
DHR ? ? ? Sg 34.973 46.222 1136121390 1136121382
MRD ? ? ? Pn 37.762 48.163 1136123668 1136123673

now i want to split these lines to N sections with these conditions:
compare 2 tandem lines,
if the absolute difference value between 6th field of those lines more than 1, and the absolute difference value between 7th field of those lines more than 1, and the absolute difference value between 9th field of those lines more than 5, then make a new section by printing "###". so between each section we'll have "###".
desired out put of this input would be:
Code:
AZR ? ? ? Pn 37.202 48.82 1136119044 1136119009
SHB ? ? ?   Pn 37.802 48.02 1136119047 1136119008
MRD ? ? ? Pn 37.102 48.42 1136119048 1136119005 
###
DHR ? ? ? Pg 34.933 46.902 1136121387 1136121382
DHR ? ? ? Sg 34.973 46.222 1136121390 1136121382
###
MRD ? ? ? Pn 37.762 48.163 1136123668 1136123673

# 2  
Old 10-14-2012
Code:
awk '{if(NR>1){if(sqrt(($6-s)*($6-s)) > 1 && sqrt(($7-p)*($7-p)) > 1 && sqrt(($9-q)*($9-q)) > 5){print "###"}}}{
s=$6;p=$7;q=$9}1' file


Last edited by pamu; 10-14-2012 at 12:11 PM.. Reason: corrected.
These 2 Users Gave Thanks to pamu For This Post:
# 3  
Old 10-14-2012
Pamu's solution is quite elegant. For those that have forgotten their high school maths:

if a * a = 16, a = 4
but a could also be -4 because -4 * -4 = 16 also

For any real number a, sqrt(a * a) = |a|

sqrt(a * a) = sqrt(16) = 4 = |-4|
# 4  
Old 10-14-2012
Quote:
Originally Posted by fpmurphy
Pamu's solution is quite elegant...
Absolutely. But it still can be simplified: If the square root is > 1, so is the radicand. Going one step further, you can use the square of the 5 as well and drop the sqrt totally. And you could omit the if statement:
Code:
awk     'NR>1 && ($6-s)*($6-s) > 1 && ($7-p)*($7-p) > 1 && ($9-q)*($9-q) > 25 {print "###"}
         {s=$6;p=$7;q=$9}
         1
        ' file

And using a function might help with readability:
Code:
awk     'function absdiff (A, B, C) {return (A-B)*(A-B) > C*C}
         NR>1 && absdiff ($6, s, 1) && absdiff ($7, p, 1) && absdiff ($9, q, 5) {print "###"}
         {s=$6;p=$7;q=$9}
         1
        ' file


Last edited by RudiC; 10-14-2012 at 02:14 PM.. Reason: typo
# 5  
Old 10-14-2012
Quote:
Originally Posted by RudiC
And using a function might help with readabilty:
Code:
awk     'function absdiff (A, B, C) {return (A-B)*(A-B) > C*C}
         NR>1 && absdiff ($6, s, 1) && absdiff ($7, p, 1) && absdiff ($9, q, 5) {print "###"}
         {s=$6;p=$7;q=$9}
         1
        ' file

The version of awk I've got installed (4.0) requires function names and opening parens to not be separated with a space. Other than that -- very nice.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count lines in section

I am tiring to cont numbers of line between the "!" in CISCO routers I have no problem to extract the input and change the empty line with ! ! 5 Cable5/0/1 U0 4 5 Cable5/0/1 U1 4 ! 5 Cable5/0/1 U2 4 ... (4 Replies)
Discussion started by: sharong
4 Replies

2. Shell Programming and Scripting

Perl split string separated by special character

Hello I have string (string can have more sections) LINE="AA;BB;CC;DD;EE"I would like to assigne each part of string separated by ";" to some new variable. Can someone help? (4 Replies)
Discussion started by: vikus
4 Replies

3. Shell Programming and Scripting

Split File based on different conditions

I need to split the file Conditions: Ignore any record that either starts with 1 or 9 Split the file at position 404 , if position 404 is abc or def then write all the records in a file > File 1 , the remaining records should go in to a file > File 2 Further I want to split the... (7 Replies)
Discussion started by: protech
7 Replies

4. Shell Programming and Scripting

Split text separated by ; in a column into multiple columns

Hi, I need help to split a long text in a column which is separated by ; and i need to print them out in multiple columns. My input file is tab-delimited and has 11 columns as below:- aRg02004 21452 asdfwf 21452 21452 4.6e-29 5e-29 -1 3 50 ffg|GGD|9009 14101.10 High class -node. ; ffg|GGD|969... (3 Replies)
Discussion started by: redse171
3 Replies

5. Shell Programming and Scripting

How to split the comma separated file?

Hi, I have a filein unix like ABC,CDE BCD,KHL and the output i need is like column1 column2 ABC,CDE ABC ABC,CDE CDE BCD,KHL BCD BCD,KHL KHL. Can some body help me out? Hi, The code is working fine. But in my file each row does not have always 1 comma. It may... (6 Replies)
Discussion started by: jagdishrout
6 Replies

6. Shell Programming and Scripting

Adding a lines to specific section of the file.

Hello, I have to a add 2 lines to /etc/sudoers file under this section below, can someone please suggest script to add these two lines when execute this remotely on to a multiple servers. before ## Allow root to run any commands anywhere root ALL=(ALL) ALL After ## Allow root... (2 Replies)
Discussion started by: bobby320
2 Replies

7. Shell Programming and Scripting

take a section of a data with conditions

I have a data file like below: 2011 0701 2015 21.2 L 37.692 46.202 18.0 Teh 4 0.3 2.1 LTeh 1 GAP=233 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 TBZ SN EPg 0 2015 31.19 -0.3... (3 Replies)
Discussion started by: saeed.soltani
3 Replies

8. Shell Programming and Scripting

How can i comment out a section between two particular lines

I want to find out which files under /etc have the the following section: and then i would like to comment out the above section in all the files. Please help. (3 Replies)
Discussion started by: proactiveaditya
3 Replies

9. Shell Programming and Scripting

AWK:Split fields separated by semicolon

Hi all, I have a .vcf file which contains 8 coulmns and the data under each column as shown below, CHROM POS ID REF ALT QUAL FILTER INFO 1 3000012 . A G 126 ... (6 Replies)
Discussion started by: mehar
6 Replies

10. Shell Programming and Scripting

Removing Duplicate Lines per Section

Hello, I am in need of removing duplicate lines from within a file per section. File: ABC1 012345 header ABC2 7890-000 ABC3 012345 Header Table ABC4 ABC5 593.0000 587.4800 ABC5 593.5000 587.6580 <= dup need to remove ABC5 593.5000 ... (5 Replies)
Discussion started by: petersf
5 Replies
Login or Register to Ask a Question