Relationships between adjacent digits


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Relationships between adjacent digits
# 1  
Old 09-14-2009
Relationships between adjacent digits

I have a long list of numbers; let's call them integers. Here is an excerpt:

01072523
0371
037408
0382028
038224
01

I want to find integers that contain a certain pattern that I'll describe as a "relationship between adjacent digits".

The pattern is a triplet:

digit digit digit

where:

#1 the relationship between the first and second digit is an increment of at least 3 and

#2 the relationship between the second and third digit is also an increment of at least 3 and

#3 the first digit in the triplet is not at "position 1" in the integer i.e. exclude the first digit of the integer i.e. exclude the first triplet.

Examples:

If integer is 01072523
then two matching triplets

01072523
...^^^

01072523
....^^^

If integer is 037408
then three matching triplets

037408
.^^^

037408
..^^^

037408
...^^^

(The digit at position 1 in the integer 037408 is "0" and the first triplet "037" is excluded from the pattern matching according to rule #3 above.)

If I can use Bourne sh and awk that would be the best.

Cheers!
# 2  
Old 09-14-2009
what have you tried??
sounds like homework to me
# 3  
Old 09-14-2009
Quote:
Originally Posted by vidyadhar85
what have you tried??
sounds like homework to me
I guess I write too much like a professor. Smilie I triggered the homework alert.

This can't be homework because I'm not studying Comp Sci or taking any UNIX courses. I already have several degrees; alas none in Maths, Physics, Info Sci or any degree that required it. I am self-taught.

I learn via simple examples, oftentimes from sites like this one.

I must be doing OK if I can write questions that could qualify as UNIX homeworks. Knowing how to ask the right questions is the biggest challenge in learning.

Cheers

Last edited by uiop44; 09-14-2009 at 07:50 PM..
# 4  
Old 09-14-2009
If it's not homeworks, indeed, it very looks like...any problem can be solved with bash, using any other external program...when you know exactly what it's about...
This is mathematics puzzle to me, and as such I won't be helpful.
It's not undoable: parsing each line of the file, getting for each line what section(s) of it you want to keep, using arithmetics evaluation to compare with what section of the line...

When I see it 'bash coded', I might understand...
...by now, I suggest maintainers/moderators to transfer this post to a more apropriate place such as 'experts...', erasing my message.

wouldn't you?
# 5  
Old 09-14-2009
Quote:
Originally Posted by daPeach
If it's not homeworks, indeed, it very looks like...any problem can be solved with bash, using any other external program...when you know exactly what it's about...
This is mathematics puzzle to me, and as such I won't be helpful.
It's not undoable: parsing each line of the file, getting for each line what section(s) of it you want to keep, using arithmetics evaluation to compare with what section of the line...

When I see it 'bash coded', I might understand...
...by now, I suggest maintainers/moderators to transfer this post to a more apropriate place such as 'experts...', erasing my message.

wouldn't you?
Thanks for some perspective dapeach. I'm flattered that you guys think I sound like a UNIX professor.

I was thinking this could be done simply and was just a matter of me not knowing the shell arithmetic functions and use of conditionals.

Perhaps it's more complex than I thought. Maybe it could it be done with string comparison (since there are only so many matching triplet possibilities)?

I'll leave this question up for a few more hours. If no one answers I'll remove it.

I apologise if this question is not really a good one for unix.com (i.e. it wouldn't really be a good learning exercise). I didn't think about it enough. But I did make it sound like a homework assigment... so maybe I get a +1 for writing style.
# 6  
Old 09-14-2009
OK OK!!!! I agree its not home work questionSmilie
but i didn't understand how 072 is triplet in 01072523??
you mean 0-7>3 and 7-2>3 {first-sec or sec-first>3}???
if so below code will work.. I know it can be done much smaller way..but for better understanding i wrote like thisSmilie
Code:
awk '{len=length($0)}{if(len>3){for(i=2;i<=len-2;i++)
{first=substr($0,i,1);sec=substr($0,i+1,1);third=substr($0,i+2,1);
if(((sec-first)>=3 || (first-sec)>=3) && ((third-sec)>=3 || (sec-third)>=3)) {print substr($0,i,3)}}}}' filename


Last edited by vidyadhar85; 09-15-2009 at 06:33 PM..
# 7  
Old 09-14-2009
Alternatively ksh or bash with a file called 'numbers'
Code:
#!/bin/ksh
while read number; do
  for (( i=4; i<=${#number}; i++ )) ; do
    (( incr1=${number:i-3:1}-${number:i-2:1}))
    (( incr2=${number:i-2:1}-${number:i-1:1}))
    if (( ${incr1#-} >= 3 && ${incr2#-} >= 3 )); then
      echo $number
      echo ${number:i-3:3}
    fi
  done
done<numbers

 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed / awk script to delete the two digits from first 3 digits

Hi All , I am having an input file as stated below 5728 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r03_q_reg_20_/Q 011 611 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r04_q_reg_20_/Q 011 3486... (4 Replies)
Discussion started by: kshitij
4 Replies

2. Shell Programming and Scripting

Find filenames with three digits and add zeros to make five digits

Hello all! I've looked all over the internet and this site and have come up a loss with an easy way to make a bash script to do what I want to do. I have a file with a naming convention as follows: 2012-01-18 string of words here 123.jpg 2012-01-18 string of words here 1234.jpg 2012-01-18... (2 Replies)
Discussion started by: Buzzman25
2 Replies

3. Shell Programming and Scripting

Adjacent row and column check in Perl

HI, i am new to perl world. And i am trying to compress a file, as given below procedure. INPUT FILE: 1 1 2 1 ==> R1 2 1 3 1 ==> R2 3 1 4 1 ==> R3 OUTPUT FILE: 1 1 4 1 (3 Replies)
Discussion started by: vasanth.vadalur
3 Replies

4. UNIX for Dummies Questions & Answers

help to identify duplicate columns adjacent value

Hi friends, I have a xlsheet like below first column having id ABCfollowed by 7digit numbers and the next column have title against the ids. Titles are unique and duplicateboth, but ids are unique even for duplicate title.Now I need to identify those duplicate title having the highest id for... (9 Replies)
Discussion started by: umapearl
9 Replies

5. Shell Programming and Scripting

Merging Adjacent Lines Using Gawk

Hi all, I have a text file consisting of 4 columns. What I am trying to do is see whether column 2 repeats multiple times, and collapse those repeats into one row. For example, here is a snippet of the file I am trying to analyze: 1 Gamble_Win 14.282 0.502 1 Sure_Thing 14.858 0.174 1... (4 Replies)
Discussion started by: Jahn
4 Replies

6. Shell Programming and Scripting

How to calculate the difference between two adjacent columns?

Dear All, I need to find the difference between two adjacent columns. The file is having 'i' columns and i need to find the difference between two adjacent columns (like $1 difference $2; $2 difference $3; .... and $(i-1) difference $i). I have used the following coding awk '{ for (i=1; i<NF;... (7 Replies)
Discussion started by: Fredrick
7 Replies

7. Shell Programming and Scripting

help: single digits inflated to 2 digits

Hi Folks Probably an easy one here but how do I get a sequence to get used as mentioned. For example in the following I want to automatically create files that have a 2 digit number at the end of their names: m@pyhead:~$ for x in $(seq 00 10); do touch file_$x; done m@pyhead:~$ ls file*... (2 Replies)
Discussion started by: amadain
2 Replies

8. UNIX for Dummies Questions & Answers

print adjacent lines

how do you print the lines before and after the line you are interested in? Example: Line to be printed: line 344 Output: line 343 line 344 line 345 Thanks (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question