Printing the intermediate integer values


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Printing the intermediate integer values
# 1  
Old 07-23-2015
Printing the intermediate integer values

Dear Help,

I have an input file which looks like -
Code:
121 300
122 345
124 567
127 234

$1 has 125 and 126 missing. How can I output those missing values?

Thanks
# 2  
Old 07-23-2015
I don't understand. Why are 125 and 126 considered missing, but not 123? What output are you hoping to get for the 2nd field?

What have you tried?

What operating system and shell are you using?
# 3  
Old 07-23-2015
Hello Don,
My bad ....you are right that 123,125,126 are missing. So, I am thinking if these values come out of the input file by any script or so.
Thanks a lot

---------- Post updated at 11:52 PM ---------- Previous update was at 11:50 PM ----------

Hi Don,
$2 is not important , I am just thinking if I get an output which looks like 123,125,126.
Thanks
# 4  
Old 07-23-2015
Hello Indra2011,

Following may help you in same.
Code:
awk 'NR>1{D=$1-A;}{if(D>1){for(i=1;i<D;i++){print A+i " is missing"}}};{A=$1}' Input_file

Output will be as follows.
Code:
123 is missing
125 is missing
126 is missing

EDIT: Above solution will work when all 1st columns are sorted so if 1st column is not sorted then you can do following too for same.
Code:
Input_file an example:
cat test57
121 300
122 345
124 567
127 234
112 222
  
sort -k1 test57 | awk 'NR>1{D=$1-A;}{if(D>1){for(i=1;i<D;i++){print A+i " is missing"}}};{A=$1}'

Output will be as follows.
Code:
113 is missing
114 is missing
115 is missing
116 is missing
117 is missing
118 is missing
119 is missing
120 is missing
123 is missing
125 is missing
126 is missing

Thanks,
R. Singh

Last edited by RavinderSingh13; 07-23-2015 at 02:35 AM.. Reason: Added one more solution for same
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 07-23-2015
Many thanks Ravinder
# 6  
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)
# 7  
Old 07-23-2015
If unsorted input is possible, you could also try something like:
Code:
awk '
NR == 1 {
	v[m = M = $1]
	next
}
{	v[$1]
	if($1+0 < m)
		m = $1
	else	if($1+0 > M)
			M = $1
}
END {	for(i = m + 1; i < M; i++)
		if(!(i in v)) {
			printf("%s%d", s, i)
			s = ","
		}
	if(s == ",")
		print "."
}' test57

which, using RavinderSingh13's test57 sample input file, produces the output:
Code:
113,114,115,116,117,118,119,120,123,125,126.

just using awk without needing to invoke sort.

Note that if the number of digits in the values in the first field is not constant and you use the code Ravinder and Akshay suggested for unsorted input, you should change the sort command from:
Code:
sort -k1 test57

or:
Code:
sort  file2

, respectively, to:
Code:
sort -n test57

or:
Code:
sort -n file2

, respectively, to force a numeric sort rather than an alphanumeric sort.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question