Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Identify records with null character

Status
Not open for further replies.

kasparov

Programmer
Feb 13, 2002
203
GB
Some records in my database have got corrupted - when I extract the data to a text file I discover that some records have the null character (\0) in them. My question is how can I easily determine which records have the null char. What I've done so far is:

od -c <datafile>

This works but it's not easy to identify the record. Is there a way to use grep, sed, or anything else to identify these corrupted records? Everything I try seems to either treat every line or none of them as including the char.

TIA, Chris
 
interesting question.
/bin/od is OK and if can live with it's
(stupid) output

od -c file | sed -ne 's/\\0/ERROR/p'

this works. following code gives you a better output

--------------------------------

/* jean-michel.sarbach@ubs.com */

/* to compile: cc -o exec-name nod.c */
/* usage: exec-name filename */
/* quiq && dirty script to reformat od's output, marking special characters */

#include <stdio.h>

#define MaxBuff 128 /* od's output is max 80 char */
#define ERRO &quot;***ERROR***&quot;

int main(int argc, char **argv)
{
FILE *in;
char *ppp, buff[MaxBuff+1];
int space, special, input;

if(!--argc) exit(1); ++argv;

/* we don't need to rewrite 'od' exec it and capture the output */

sprintf(buff,&quot;/bin/od -c %s&quot;,*argv); in = popen(buff,&quot;r&quot;);

while(fgets(buff,MaxBuff,in)){

input = special = space = 0;

for(ppp = buff+7; *ppp && *ppp != '\n'; ++ppp){

++input;
switch(*ppp){
case ' ':
if(3 > space++) break;
space = 0;
printf(&quot; &quot;);
break;
case '\\':
if(2 == space){ special = 1; break; }
special = space = 0;
printf(&quot;\\&quot;);
break;
default :
space = 0;
if(!special){ printf(&quot;%c&quot;,*ppp); break; }
special = 0;
switch(*ppp){
case 'n': printf(&quot;\n&quot;); break;
case 't': printf(&quot;\t&quot;); break;
case '0': printf(ERRO); break;
default: break;
}
break;
}
}
}
pclose(in);
if(input) printf(&quot;\n&quot;);
exit(0);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top