Awk Print Only Continous Numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk Print Only Continous Numbers
# 1  
Old 02-10-2011
Awk Print Only Continous Numbers

Hi,

i need help to print only those numbers which occur next to each other from a file.

Input:

Code:
1
2
3
9
44
45
46
77
79
80
81

Desired Output:

Code:
1
2
3
--
44
45
46
--
79
80
81

Thank you!
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 02-10-2011 at 12:01 PM.. Reason: code tags, please!
# 2  
Old 02-10-2011
Hi
Try this:


Code:
$ awk '{a[j++]=$0;}END{for (i=0;i<j;i++){ if (a[i]==a[i+1]-1 || a[i]==a[i-1]+1)print a[i]; else print "--";}}' file
1
2
3
--
44
45
46
--
79
80
81


Guru
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 02-10-2011
Quote:
Originally Posted by guruprasadpr
Hi
Try this:


Code:
$ awk '{a[j++]=$0;}END{for (i=0;i<j;i++){ if (a[i]==a[i+1]-1 || a[i]==a[i-1]+1)print a[i]; else print "--";}}' file
1
2
3
--
44
45
46
--
79
80
81


Guru
Dear Guru,

The script works but not completely, especially when if two different sets of consecutive numbers occur next to each other. For example, if i omit number '9' in this Input, then 1,2,3,44,45,46 are printed together. Can you help?
# 4  
Old 02-10-2011
try:

Code:
|awk -v x=1 'NR==1{n=$0}
$0==n+1{length(a[x])>0?a[x]=a[x]"\n"$0:a[x]=n"\n"$0;n=$0}$0>n+1{n=$0;x++;}
END{for(i=1;i<=x;i++) print (a[i]?a[i]:"--")}' urfile

This User Gave Thanks to yinyuemi For This Post:
# 5  
Old 02-10-2011
Hi Yinyuemi,

As i had mentioned before, if i omit the 9 in the input file, which i may have to do at time, then the output is printed like this:
Code:
1
2
3
44
45
46
--
79
80
81

But actually the desired output even when number '9' is omitted in the file is:
Code:
1
2
3
--
44
45
46
--
79
80
81

Thank you for your support. Since I don't know much about awk any solution will be very helpful.
# 6  
Old 02-10-2011
try:
Code:
awk -v x=1 'NR==1{n=$0}
$0==n+1{length(a[x])>0?a[x]=a[x]"\n"$0:a[x]=n"\n"$0;n=$0}$0>n+1{n=$0;x++;}
END{for(i=1;i<=x;i++) {if(a[i])print a[i]"\n--"}}' file

This User Gave Thanks to yinyuemi For This Post:
# 7  
Old 02-10-2011
Perfectly works! Thanks a lot Yinyuemi!

---------- Post updated at 05:59 PM ---------- Previous update was at 05:56 PM ----------

And one more little help! Can you also tell me how can i print each consecutive set of numbers to different files.
For example:
Code:
1
2
3
--
44
45
46
--

Print each set, say to file1 and file2.
Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk- Indexing a list of numbers in file2 to print certain rows in file1

Hi Does anyone know of an efficient way to index a column of data in file2 to print the coresponding row in file1 which corresponds to the data in file2 AND 30 rows preceding and after the row in file1. For example suppose you have a list of numbers in file2 (single column) as follows:... (6 Replies)
Discussion started by: Geneanalyst
6 Replies

2. Shell Programming and Scripting

Print range of numbers

Hi I am getting an argument which specifies the range of numbers. eg: 7-15 Is there a way that i can easily (avoiding loop) print the range of number between and including the specified above. The above example should translate to 7,8,9,10,11,12,13,14,15 (3 Replies)
Discussion started by: tostay2003
3 Replies

3. Shell Programming and Scripting

How to ignore characters and print only numbers using awk?

Input: ak=70&cat15481=lot=6991901">Kaschau (1820-1840) ak=7078&cat15482=lot=70121">Principauté (1940-1993) ak=709&cat=lot15484=70183944">Arubas (4543-5043)Output: 70 15481 6991901 7078 15482 70121 709 15484 70183944 (11 Replies)
Discussion started by: sdf
11 Replies

4. Shell Programming and Scripting

Print lines with numbers only.

Is there other way then this only solution I have found to print lines with numbers only? file.txtone two three 3 four five 5tt 6gfjdfgdf 9 66 six 0 seven 7 eight 546546 gffdg445gfg fd644 (7 Replies)
Discussion started by: Jotne
7 Replies

5. UNIX for Dummies Questions & Answers

Print last row of numbers

I have a spreadsheet of extremely long rows of numbers. I want to print only the last column. Tried using printf but there seems to be too many rows. example: 3 100 34 7 23 0 8 ..... X 400 203 778 1 ..........Y 58 3 9 0 100 ..........Z I only want to print X, Y and... (1 Reply)
Discussion started by: jimmyf
1 Replies

6. Shell Programming and Scripting

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 ... (7 Replies)
Discussion started by: AK47
7 Replies

7. UNIX for Dummies Questions & Answers

Print numbers and associated text belonging to an interval of numbers

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

8. Shell Programming and Scripting

print lines containing only numbers

Hi boys, I have a txt file with a lot of lines. It have lines containing mostly only numbers but some of them contain numbers mixed with special characters and or letters or space.. its look like this: 271261621371 2727127f 27126|71372. ... Do you have any ideas of the easiest way to... (3 Replies)
Discussion started by: alekkz
3 Replies

9. Shell Programming and Scripting

iterate through list of numbers and print specific lines with awk

Could someone please point me in the right direction with the following? I have a program that generates logs that contains sections like this: IMAGE INPUT 81 0 0.995 2449470 0 1726 368 1 0.0635 0.3291 82 0 1.001 2448013 0 1666 365 1 0.0649 ... (4 Replies)
Discussion started by: euval
4 Replies

10. Shell Programming and Scripting

Query to print numbers in words

Hi, I have to write a shell script that converts numbers in to words below is what i wrote.My script is not running. ----------------------------------- echo -n "Enter number : " read n len= echo $n | wc -c echo " number in words : " for ( i=1; i<len; i++ ) do num=echo $n... (5 Replies)
Discussion started by: bab123
5 Replies
Login or Register to Ask a Question