I am trying to replace nulls with spaces in every record of a '|' delimited file. I used this command -
cat input.dat | sed 's/||/| |/g' > output
But if a space appears twice '| | |' , then only the first null is getting replaced with a space. The second one remains as a null. I guess, this may be because I am not specifying the delimiter and checking for nulls. Can anyone help me out in this. Thanks a lot..!!!
I read that answer with interest. It does answer the OP's question.
But just on a side issue, why do we have to do this twice? Is it because the way sed works is that it will not reoperate on something it has just worked on in the same command?
i.e.
for the line:
|||
the command
sed 's/||/| |/g'
works by doing
| ||
and because the second | has been operated on, sed will not evaluate it, and therefore the new line containing || is not picked up. Is this right?
But, having to do it twice will replace two consecutive nulls. This means that we have to repeat this operation as many times as the maximum number of consecutive nulls appearing in the row.
Is there a way to make it replace nulls by giving it the delimiter '|' and asking it to replace null values?
I dont quite get what you are saying. But running that sed statement once will replace all null's with a whitespace. All you need is 's/||/| |/g;s/||/| |/g'.