Rahul,
It sounds like you're trying to NUL terminate the first (whitespace delimited) field (word) of each line in a text file.
That's a very odd request (because the ASCII NUL character is normally used to terminate strings ... but normally NOT embedded in text files ... so many tools that might be trying to read the file line by line would not handle the NUL character gracefully.
It's also possible that your shell or your copy of
sed or whatever cannot handle this cleanly. So you might need to use
GNU versions of these tools, or a copy of
Perl or Python (or compile up a little utility in C, of course).
The most obvious attempt in plain
bash would be:
Code:
#!/bin/bash
while IFS="" read line; do
set -- $line
first_word="$1"
shift
echo -e "$firstword\000$*"
done
This is written as a filter so you pipe you file through it. To verify that it's doing what you want you can pipe the output further through a command like
cat -A or
od -x to be sure it shows the NUL characters where you want them.
That might have some odd artifacts (due to the way that each line is parsed by the
set -- command). This following one-liner works in two stages, using
sed the first space on each line to a character "177" (octal) --- hex 0x7F, a.k.a. the "DEL" character; and then using the
tr command to change that into an ASCII NUL:
Code:
sed -e 's/ /'$(echo -ne '\177')'/' /tmp/foo | tr '\177' '\00' | cat -A
This assumes that the original file has no ASCII DEL character that you care about preserving ... and the example shows a
cat -A just for your convenience. You'd replace that with an appropriate redirection to save your output.
JimD (former Linux Gazette AnswerGuy)