Dear Help,
I have an input file which looks like -
121 300
122 345
124 567
127 234
$1 has 125 and 126 missing. How can I output those missing values?
Thanks
Dear Help,
I have an input file which looks like -
121 300
122 345
124 567
127 234
$1 has 125 and 126 missing. How can I output those missing values?
Thanks
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?
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
Hello Indra2011,
Following may help you in same.
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.
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.
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.
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
Many thanks Ravinder
[akshay@localhost tmp]$ cat file
121 300
122 345
124 567
127 234
[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
[akshay@localhost tmp]$ cat file2
121 300
122 345
124 567
127 234
112 222
[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
awk 'FNR>1 && $1!=p+1{for(i=p+1;i<=$1-1;i++)print i}{p=$1}'
$1 is the first column from current input linep is the previous value of the last lineFNR Number of Records relative to the current input fileFNR >1 && $1!=p+1 is a condition : if FNR>1 and $1 is different than previous value +1 , thenfor(i=p+1;i<=$1-1;i++) loop from previous record value +1 to current record value -1, and print i which is missing{p=$1} is executed for each lines : p holds current record's 1st column value ( $1 )If unsorted input is possible, you could also try something like:
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:
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:
sort -k1 test57
or:
sort file2
, respectively, to:
sort -n test57
or:
sort -n file2
, respectively, to force a numeric sort rather than an alphanumeric sort.