Visit Our UNIX and Linux User Community

Cutting lines if conditions are met

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Cutting lines if conditions are met
# 1  
Old 12-20-2011
Cutting lines if conditions are met

I am not sure how to solve this problem and if it is possible to do so with scripting. Smilie

Let's say I have this data:

A 12345 12360
A 12359 12380
A 12381 12390
A 12400 12450
A 12451 12460
B 23456 23460
B 23470 23480
B 23477 23505

I wan't each line to be compared in this way: If the values of column 2 and 3 of line 1 overlap values of column 2 and 3 of line 2 then keep first line only.
Then keep comparing.
So my result would be:

A 12345 12360
A 12381 12390
A 12400 12450
A 12451 12460
B 23456 23460
B 23470 23480

Thanks for your input!
# 2  
Old 12-20-2011
Should it consider all previous lines, or only just the previous line?
# 3  
Old 12-20-2011
Only the very last one!
# 4  
Old 12-20-2011
Okay, that's doable:

awk '(NR>1) && (X2>=$2) && ($3>=X1) { next }; { X1=$2; X2=$3 } 1' data

Which amounts to:

awk '
# If this isn't the first line, and it intersects the range X1-X2, skip it.
(NR>1) && (X2>=$2) && ($3>=X1) { next }
# record the new range and print
{ X1=$2; X2=$3 } 1' data

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 12-20-2011
Thank you so much.
It seems to be working though I don't really follow how the program is working.
Is it comparing e.g.
Line 2 to Line 1. If there is no overlap, it keeps both lines and then compares line 3 to line 2. Then line 3 overlaps with line 2 so it kicks line 3 out and then compares line 4 to line 2?

---------- Post updated at 03:19 PM ---------- Previous update was at 02:42 PM ----------

Oh yes, and another question: How would the code be different if it were to consider all previous lines.
# 6  
Old 12-22-2011
It would potentially need to store values for each and every line to compare later.

This could be an awful lot of computational work, n^2 intersection checks for n lines.

---------- Post updated at 12:47 PM ---------- Previous update was at 12:29 PM ----------

$ cat allset.awk
{       for(N=1; N<=MAX; N++) if(($3 >= X1[N]) && (X2[N]>=$2))  next; }
{       MAX++; X1[MAX]=$2; X2[MAX]=$3; } 1
$ awk -f allset.awk < data
A 12345 12360
A 12381 12390
A 12400 12450
A 12451 12460
B 23456 23460
B 23470 23480


Previous Thread | Next Thread
Test Your Knowledge in Computers #407
Difficulty: Medium
Cygwin's default package selection is quite small, containing little more than the bash shell and the core file manipulation utilities expected of a Unix command line.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Cutting specific columns from lines

I am trying to remove columns 81-97 from a line that can be as long as 114 characters. Because a number of lines might not have under 80 characters, using the cut command following by paste could be a problem. While sed might work, is there some other utility that could do this more easily? ... (9 Replies)
Discussion started by: wbport
9 Replies

2. Shell Programming and Scripting

Comparing all lines in a column with another is condition is met

Sorry for this noob question, I have file with 4 columns like where columns 2 and 4 have numbers a 55 k 3 b 59 l 3 c 79 m 277 d 255 n 277 e 257 o 267 f 267 p 287 g 290 q 287 h 290 r 287 i 310 s 900 now i want to select only those rows, where values in column 4 are greater than... (4 Replies)
Discussion started by: amits22
4 Replies

3. Shell Programming and Scripting

Cutting fields from lines with multiple spaces

Please see the following code, between "status" and "OK" exists many spaces, I want to get status OK . how to ignore multi spaces? If tab exists in the spaces, how to ignore it ? Is there other commands can replace cut? $ echo 'drv status OK'| cut... (3 Replies)
Discussion started by: 915086731
3 Replies

4. Shell Programming and Scripting

create folders until conditions met

Hi, I want to create 10 folders, starting from TEST001 till TEST010, after folder TEST010 is created the script should exit. Thanks in advance (1 Reply)
Discussion started by: bprabhukumar
1 Replies

5. Shell Programming and Scripting

cutting lines

Dear All, Is there a way to cut the lines that have been "head" Here is what i m trying to do Please advice there is file name dummy.txt now i am trying to head this file 4 time by using a loop and every time this file is head with different values e.g in first instance it will... (7 Replies)
Discussion started by: jojo123
7 Replies

6. Shell Programming and Scripting

Cutting specific lines from a file

Hi, I have a file named Mani.txt. The contents are like this cat Mani.txt -------------------------------------------------------- Hi there how r u My Name is Mani Bye ------------------------------------------------------------ I want to cut the first and last lines from the file... (15 Replies)
Discussion started by: pathanjalireddy
15 Replies

7. Shell Programming and Scripting

need help cutting consecutive lines with sed or awk

HI All, I want to cut 5 lines after a pattern using sed or awk. can any one tell me how to do it ? (2 Replies)
Discussion started by: raghin
2 Replies

8. UNIX for Dummies Questions & Answers

Cutting the top two lines, and also charachters below.

Hey all. I have a file that I am trying to cut information out of. We have a script that shows us all of our Radio Scanners that are being used and I'm writing a script that clears all of the context off of the scanners. The script that runs shows us this information below... |emp_id ... (5 Replies)
Discussion started by: jalge2
5 Replies

9. UNIX for Dummies Questions & Answers

Cutting n consecutive lines from a file...

Hi, I have this problem of separating 10 consecutive lines from a file, say starting from 21 to 30... I have used a filter like this.. head -n 30 myfile | tail -n 10 Is there a simpler way than this? (2 Replies)
Discussion started by: Vishnu
2 Replies

10. UNIX for Dummies Questions & Answers

Cutting lines out using sed I presume.

Happy New Year!!! I'm trying to cut out some text in a data file where I only want the first line and the last line. For example. 1. Colin Was here <<-- Text I want to cut out 2. THIS IS THE TEXT I WANT TO CUT <- CUT THIS OUT 3. OUT BECAUSE IT'S NO GOOD TO ME <- CUT THIS OUT 4. I... (5 Replies)
Discussion started by: cfoxwell
5 Replies

Featured Tech Videos