Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Printing the intermediate integer values Post 302950268 by Akshay Hegde on Thursday 23rd of July 2015 02:52:38 AM
Old 07-23-2015
Code:
[akshay@localhost tmp]$ cat file
121 300
122 345
124 567
127 234

Code:
[akshay@localhost tmp]$ awk 'FNR>1 && $1!=p+1{for(i=p+1;i<=$1-1;i++)print i}{p=$1}' file
123
125
126

Code:
[akshay@localhost tmp]$ cat file2 
121 300
122 345
124 567
127 234
112 222

Code:
[akshay@localhost tmp]$ sort  file2 | awk 'FNR>1 && $1!=p+1{for(i=p+1;i<=$1-1;i++)print i}{p=$1}'
113
114
115
116
117
118
119
120
123
125
126

---------- Post updated at 01:22 PM ---------- Previous update was at 01:10 PM ----------

Explanations

Code:
awk 'FNR>1 && $1!=p+1{for(i=p+1;i<=$1-1;i++)print i}{p=$1}'

  1. $1 is the first column from current input line
  2. p is the previous value of the last line
  3. FNR Number of Records relative to the current input file
  4. so FNR >1 && $1!=p+1 is a condition : if FNR>1 and $1 is different than previous value +1, then
  5. for(i=p+1;i<=$1-1;i++) loop from previous record value +1 to current record value -1, and print i which is missing
  6. {p=$1} is executed for each lines : p holds current record's 1st column value ($1)
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help: How do I ADD non-integer (decimal) values?

I am trying to create a script that will read from a file two non-integer values (decimals) and add those values together. For example, I want to add 1.51 and -2.37 together and get the sum. Any ideas? Thanks! (2 Replies)
Discussion started by: limshady411
2 Replies

2. Programming

to compare two integer values stored in char pointers

How can I compare two integer values which is stored in char pointers? suppose I have char *a and char *b having values 10 and 20. how can i find the shorter value? (1 Reply)
Discussion started by: naan
1 Replies

3. UNIX for Advanced & Expert Users

Printing defaulted values

I have written a phyton script that accepts command line arguments. I am setting defaults for some of them if the user does not specify them. However I want to print the user values and the defaulted values seperately. However, once I set the default values, then I cannot use if... (0 Replies)
Discussion started by: kristinu
0 Replies

4. Shell Programming and Scripting

printing two values with TAB in between

Dear friends, I want to print variables' values in certain format where space between two values of variables is "a tab" I tried where I provided "tab" between two varibales. But when it print values on screen its giving me output without spaces in two values. Request you to help me in... (7 Replies)
Discussion started by: anushree.a
7 Replies

5. UNIX for Dummies Questions & Answers

compare decimal and integer values in if in bash shell

i need to do camparisions like the below. For the case when first=10 and second=9.9 the scripts displays process failed. I need to be able to convert the values to integer before doing the comparision. Like 9.9 should be rounded over to 10 before doing comparision. Please advice how can... (3 Replies)
Discussion started by: nehagupta
3 Replies

6. Programming

Printing float values in C

Hi, I have small problem to print float value in the fallowing code float Cx, W,f=250000, Cr=92.00,pi=3.14; W=2*pi*f; Cx = 1/W.Cr; //Cx value will be come around like 7.07E-9. printf("capacitance value: %.10f",Cx); I am trying to print Cx value using above code but it was not... (3 Replies)
Discussion started by: veerubiji
3 Replies

7. UNIX for Dummies Questions & Answers

Printing all the values in the middle of two columns

Hi, I have a tab delimited text file with three columns: Input: 1 25734 25737 1 32719 32724 1 59339 59342 1 59512 59513 1 621740 621745 For each row of the text file I want to print out all the values between the second and third columns, including them. The... (3 Replies)
Discussion started by: evelibertine
3 Replies

8. Programming

Printing values from a class

I have a class and want to print values in MOD using L = new Layer* ; How can I print the values in MOD using this object L??? class Layer { public : Model* MODP; Model* MODS; (1 Reply)
Discussion started by: kristinu
1 Replies

9. Shell Programming and Scripting

Printing $values using awk

Hi All I had requirement where I need to re-order columns in a file by using a control file. here is the ctrl file c1 c2 c3 source file c3 | c1 | c2 a | b| c I should create output file based on the ctrl file columns o/p should look like this c1 | c2 | c3 b| c|a I wrote some... (9 Replies)
Discussion started by: gvkumar25
9 Replies

10. Shell Programming and Scripting

Array not printing values if used in a loop

Hello! I'm making an English to Morse Code translator and I was able to mostly get it all working by looking through older posts here; however, I have one small problem. When I run it it's just printing spaces for where the characters should be. It runs the right amount of times, and if I try... (3 Replies)
Discussion started by: arcoleman10
3 Replies
All times are GMT -4. The time now is 10:35 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy