Operating System - Linux
1753269 Members
4951 Online
108792 Solutions
New Discussion юеВ

Stickey NUL issue ... sed/grep/awk ... perl

 
SOLVED
Go to solution
A. Daniel King_1
Super Advisor

Stickey NUL issue ... sed/grep/awk ... perl

Hi, folks,

How do I (1) detect and (2) replace a mid-line null, such as:

This is a li^@ne in a text file ...

Where the ^@ is a NUL character?
Command-Line Junkie
7 REPLIES 7
RAC_1
Honored Contributor
Solution

Re: Stickey NUL issue ... sed/grep/awk ... perl

Post following.
cat "file" | od -c
cat file | vis

Would like to look at how it looks like.
There is no substitute to HARDWORK
A. Daniel King_1
Super Advisor

Re: Stickey NUL issue ... sed/grep/awk ... perl

cat /tmp/1.txt | od -c
0000000 T h i s i s \0 t h e f i l e
0000020 .
0000021

cat /tmp/1.txt | vis
This is\000the file.
Command-Line Junkie
Kent Ostby
Honored Contributor

Re: Stickey NUL issue ... sed/grep/awk ... perl

In awk, I think you could do:

{idx1=index($0,"\0");
if (idx1==0) {print $0;next}
newstring=substr($0,1,index-1)substr($0,index+1);
}

"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
RAC_1
Honored Contributor

Re: Stickey NUL issue ... sed/grep/awk ... perl

cat file | tr "\000" "\xxx"

xxx is ascii char you want to replace 000 with. man ascii for details.

Anil
There is no substitute to HARDWORK
Kent Ostby
Honored Contributor

Re: Stickey NUL issue ... sed/grep/awk ... perl

Let's try that again:

In awk, I think you could do:

{idx1=index($0,"\0");
if (idx1==0) {print $0;next}
newstring=substr($0,1,index-1)substr($0,index+1); print newstring;
}
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Hein van den Heuvel
Honored Contributor

Re: Stickey NUL issue ... sed/grep/awk ... perl

I like tr -d best myself, but it all works:

# perl -e 'printf "Thisis%cthefile\n",0' > x
# cat x
Thisisthefile
# od -c x
0000000 T h i s i s \0 t h e f i l e \n
#
# perl -pe 's/\0//' x | od -c
0000000 T h i s i s t h e f i l e \n
#
# cat x | tr -d "\000" | od -c
0000000 T h i s i s t h e f i l e \n
#
# awk '{gsub(/\000/,""); print}' x | od -c
0000000 T h i s i s t h e f i l e \n

Hein.
A. Daniel King_1
Super Advisor

Re: Stickey NUL issue ... sed/grep/awk ... perl

awk had trouble, I ended up using perl, and I really like the vis and tr solutions.
Command-Line Junkie