Print numbers between two number ranges


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print numbers between two number ranges
# 1  
Old 11-07-2012
Print numbers between two number ranges

Hi, I have a list.txt file with number ranges and want to print/save new all.txt file with all the numbers and between the numbers.

== list.txt ==
65936
65938
65942 && 65943
65945
65947 && 65949
65961
65963 && 65965
65966 && 65969
6597

after:
== all.txt ==
65936
65938
65942
65943

65945
65947
65948
65949

65961
65963
65964
65965

65966
65967
65968
65969

6597

Can you guy help me with this script in unix/linux (cygwin) ?

Thank you in advance.

BR,
# 2  
Old 11-07-2012
try:
Code:
 
awk '{for (i=$1; i<=$NF ; i++) print i}' list.txt


Last edited by rdrtx1; 11-07-2012 at 05:02 PM.. Reason: fixed range of numbers print.
# 3  
Old 11-07-2012
tnx, but I am still missing the number between &&.

I receive:
65936
65938
65942
65943
65945
65947
65949
65961
65962
65963
65965
65966
65967
65968

65969
6597
# 4  
Old 11-07-2012
sorry, try:
Code:
 
awk '{for (i=$1; i<=$NF ; i++) print i}' list.txt

# 5  
Old 11-07-2012
rdrtx1 solution is most elegant one, here is a different approach:-

Code:
awk ' {
  for (i=1; i<=NF ; i++)
  {
    if ($i == "&&")
    {
      st=$(i-1);
      en=$(i+1);
      st++;
      while (st < en)
      {
         print st;
         st++;
      }
    }
    else
        print $i;
  }
}' list.txt

# 6  
Old 11-08-2012
Code:
awk '{if(NF==1){print $1} else { for(Val=$1; Val<=$3; Val++) print Val  } } ' File_Name

---------- Post updated at 02:46 AM ---------- Previous update was at 02:27 AM ----------

Less Complex

Code:
awk '{for(Val=$1; Val<=$NF; Val++) print Val  } ' File_Name

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by radoulov; 11-08-2012 at 04:21 AM..
# 7  
Old 11-08-2012
Tnx you all very much Smilie.

bipinajith I'm receiving error with your Code.Smilie


Code:
awk ' {
  for (i=1; i<=NF ; i++)
  {
    if ($i == "&&")
    {
      st=$(i-1);
      en=$(i+1);
      st++;
      while (st < en)
      {
         print st;
         st++;
      }
    }
    else
        print $i;
  }
}' list.txt



error received:
Code:
[~]$ awk  -f ./x/extract.awk
awk: ./x/extract.awk:1: awk ' {
awk: ./x/extract.awk:1:     ^ invalid char ''' in expression
awk: ./x/extract.awk:1: awk ' {
awk: ./x/extract.awk:1:     ^ syntax error

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check/print missing number in a consecutive range and remove duplicate numbers

Hi, In an ideal scenario, I will have a listing of db transaction log that gets copied to a DR site and if I have them all, they will be numbered consecutively like below. 1_79811_01234567.arc 1_79812_01234567.arc 1_79813_01234567.arc 1_79814_01234567.arc 1_79815_01234567.arc... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

Print root number between min and max ranges

Hi to all, Please help on the following problem, I'm not where to begin, if awk or shell script. I have pairs of ranges of numbers and I need to find the root or roots of ranges based on min Range and Max ranges Example #1: If min range is 120000 and max ranges 124999, it means that are... (5 Replies)
Discussion started by: Ophiuchus
5 Replies

3. Shell Programming and Scripting

How to convert multiple number ranges into sequence?

Looking for a simple way to convert ranges to a numerical sequence that would assign the original value of the range to the individual numbers that are on the range. Thank you given data 13196-13199 0 13200 4 13201 10 13202-13207 3 13208-13210 7 desired... (3 Replies)
Discussion started by: jcue25
3 Replies

4. UNIX for Dummies Questions & Answers

Print numbers and associated text belonging to an interval of numbers

##### (0 Replies)
Discussion started by: lucasvs
0 Replies

5. UNIX for Dummies Questions & Answers

Problem with ranges of numbers

Good day to everyone! So, let's start :) I have a file with a numbers in some ranges for example: 1 10 49 72 ... and this file need to transform to: 1 2 3 4 (14 Replies)
Discussion started by: shizik
14 Replies

6. Shell Programming and Scripting

the smallest number from 90% of highest numbers from all numbers in file

Hello All, I am having problem to find what is the smallest number from 90% of highest numbers from all numbers in file. I am having file with thousands of lines and hundreds of columns. I am familiar mainly with bash but I am open to whatever suggestion witch will lead to the solutions. If I... (11 Replies)
Discussion started by: Apfik
11 Replies

7. UNIX for Dummies Questions & Answers

Awk ranges for selecting numbers

Hi, I am trying to use AWK to do some editing and formating of large tables of numbers and I am having trouble getting it to work. For brevities sake, I won't show the whole table, but I have a sample set of code: und$ awk '{($2+0) > 50;print $1}' temp 2000 147 2008 128 2002 100 1999 47... (2 Replies)
Discussion started by: ikerrin1@gmail.
2 Replies

8. Shell Programming and Scripting

Number count per number ranges

Hi, I have a question here that need to get advise from all of you. Let say I have a set of data 12347777 12359899 12347677 12360090 12347688 12359979 12359009 12367022 12346677 I need to count the number that appear in each numbering ranges and the output is like below: Prefix ... (5 Replies)
Discussion started by: shirleyeow
5 Replies

9. Shell Programming and Scripting

Finding number ranges using grep

Is it possible for me to find numbers in a file by a range using grep? like cat data | cut -f1 | grep <info> Im trying to find information and extract every amount that is less than a number (ie less than 75 or whatever) Is this possible? (2 Replies)
Discussion started by: DKNUCKLES
2 Replies

10. Shell Programming and Scripting

awk to print mon and max values of ranges

HI all I'm trying to write an awk script to print the min and max value in a range(s) contained in another file - the range values are in $2 EG 114,7964,1,y,y,n 114,7965,1,y,y,n 114,7966,1,y,y,n 114,7967,1,y,y,n 114,7969,1,y,y,n 114,7970,1,y,y,n 114,7971,1,y,y,n 114,7972,1,y,y,n... (3 Replies)
Discussion started by: Mudshark
3 Replies
Login or Register to Ask a Question