When I BCP the data in sql server
In the output file I am getting a NUL like character in the output file, and i want to replace this with the single blank space.
When I used the below sed
command it removes the NUL character but between those 2 delimiter we don't have single space.
sed 's/\x0/ /g' output file name
Example: After sed command i am getting output file like below
PHMO||P00000005233
PHMO||P00000005752
But i need a single spacing in between those delimiter as
PHMO| |P00000005233
PHMO| |P00000005752
This is an easy job for sed. Let's start creating a test file as you didn't provide one:
As you can see it contains ASCII 0:
Now let's run sed:
And check the output:
The usual approach to this would be using
tr
. However, solutions withtr
andsed
are not portable. (The question is tagged "unix", so only portable solutions are interesting).Here is a simple demo script
which I named
foo
, and its input (with the ASCII NUL shown here as^@
):Running with GNU
tr
andsed
:With OSX:
With Solaris 10 (and 11, though there may be a recent change):
Bear in mind that
sed
is line-oriented, and that ASCII NUL is considered a binary (non-line) character. If you want a portable solution, then other tools such as Perl (which do not have that limitation) are useful. For that case one could add this to the script:The intermediate tool
awk
is no better in this instance. Going to Solaris again, with these lines:I see this output:
Further reading:
it can be done quite easily with
perl