Gap length between intervals


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Gap length between intervals
# 1  
Old 09-30-2015
Gap length between intervals

hi all,

I wish to calculate the length between intervals whose are defined by a starting and an end possition. The data looks like this:

Code:
1 10
23 30
45 60
70 100

...

The desired output should be:
Code:
13 # (23-10)
15 # (45-30)
10 # (70-60)

...

I donīt know how to operate with different rows. I'm blocked.
Any suggestion?
Thank you in advance!
# 2  
Old 09-30-2015
A hint:
Code:
while read VAR1 VAR2
do
   echo $VAR2 " " $VAR1
done<your_file

With the above you should be able to figure out the next step...

Cheers
This User Gave Thanks to vbe For This Post:
# 3  
Old 09-30-2015
With a question like this, it is ALWAYS a good idea to tell us what operating system and shell you're unison. With a POSIX conforming shell, you could try something like:
Code:
#!/bin/ksh
last=""
while read x y
do	[ "$last" != "" ] && printf '%d # (%d-%d)\n' $((x - last)) "$x" "$last"
	last="$y"
done < data

which, if the file named data contains the text you showed us, produces the output:
Code:
13 # (23-10)
15 # (45-30)
10 # (70-60)

as you requested.
This User Gave Thanks to Don Cragun For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding contiguous numbers in a list but with a gap number tolerance

Dear all, I have a imput file like this imput scaffold_0 10558458 10558459 1.8 scaffold_0 10558464 10558465 1.75 scaffold_0 10558467 10558468 1.8 scaffold_0 10558468 10558469 1.71428571428571 scaffold_0 10558469... (5 Replies)
Discussion started by: valente
5 Replies

2. Shell Programming and Scripting

Flat file-make field length equal to header length

Hello Everyone, I am stuck with one issue while working on abstract flat file which i have to use as input and load data to table. Input Data- ------ ------------------------ ---- ----------------- WFI001 Xxxxxx Control Work Item A Number of Records ------ ------------------------... (5 Replies)
Discussion started by: sonali.s.more
5 Replies

3. Shell Programming and Scripting

Need to find the gap in the sequence of numbers

Hi Guys, I have a file with numbers in sequence. The sequence have been broken somewhere.. I need to find out at which number the sequence has been broken... For an example, consider this sequence, it needs to give me output as 4 (as 5 is missing) and 6(as 7 is missing) Thanks for... (3 Replies)
Discussion started by: mac4rfree
3 Replies

4. Shell Programming and Scripting

How to use while loop in bash shell to read a file with 4 lines of gap

Hi , I am currently using the while loop in bash shell, as follows. while read line do echo $line done < file.txt However, i want to use the while loop on file.txt, which will read the file with 4 lines of gap. Ex- if file.txt is a file of 100 lines, then i want to use the loop such... (3 Replies)
Discussion started by: jitendriya.dash
3 Replies

5. Shell Programming and Scripting

Decrease the Line Gap in a files and Echo a strings

Hi Expert, I have a file contains-- GET:RSUB:ISI,432350114637601; RESP:0:MDN,9352608473:ISI,432350114637601:T11,1:T21,1:T22,1:B16,1:T62,1:BAIC,0:BAOC,0:BOIC,0:BIRO,0:BORO,0:BOIH,0:BOS4,0:CLIP,1:CLIR,0:CFB,1; GET:RSUB:ISI,432350114281653; ... (5 Replies)
Discussion started by: thepurple
5 Replies

6. UNIX for Dummies Questions & Answers

Sed working on lines of small length and not large length

Hi , I have a peculiar case, where my sed command is working on a file which contains lines of small length. sed "s/XYZ:1/XYZ:3/g" abc.txt > xyz.txt when abc.txt contains lines of small length(currently around 80 chars) , this sed command is working fine. when abc.txt contains lines of... (3 Replies)
Discussion started by: thanuman
3 Replies

7. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies
Login or Register to Ask a Question