Append the end of each line in a file with a given string

Hi friends,

I have a file containing many lines as follows.

M:\mmarimut_v6.4.0_pit_01\java\build.xml@@\main\v6.4.0_pit_a
M:\mmarimut_v6.4.0_pit_01\ADBasicView.java@@\main\v6.4.0_pit_a

I would like to append the string "\0" at the end of each line in the file. The output should look like:

M:\mmarimut_v6.4.0_pit_01\java\build.xml@@\main\v6.4.0_pit_a\0
M:\mmarimut_v6.4.0_pit_01\ADBasicView.java@@\main\v6.4.0_pit_a\0

I tried this command but in vain. Please help me. Thanks in advance.

awk '{print $0,"\0"}' C:\LOC\v6.4.0_test.txt > C:\LOC\v6.4.0_awktest.txt

Escape the backslash:

awk '{print $0 "\\0"}' file > newfile

or with sed:

sed 's/.*/&\\0/' file > newfile

use below:-
in solaris use nawk or /usr/xpg4/bin/awk;
in other systems use gawk.

nawk '{$0=$0"\\O" }1' infile.txt > outfile.txt

;);):wink:

Thanks Ahmad.diab for a quick reply. I had tried to escape the back slash but it didnt work for me. I am working on windows and sed doesnt work for me, so the only option is to use awk. Any alternative just let me know. Thanks again.

Thanks,
nmattam

if you are using cygwin do the below:-

gawk '{$0=$0"\\O" }1' infile.txt > outfile.txt

note:- $0 in words is $zero

;);):wink:

Hi ahmad.diab, i tried gawk '{$0=$0[\\0](file://\\0) }1' infile.txt > outfile.txt but the result is not as expected. It appends a special character at the end. I am unable to copy the special character. And more over the string to be attached is a (\0 ie \zero). Any suggestions. Thanks.

be careful you have to put the quotation marks ("") around your string or the system will translate the \\0 to NULL string.

gawk '{$0=$0"\\0" }1' infile

:D;):o

Hi ahmad.diab, i executed the command with double quotes placed correctly, but some how during copy paste it missed. This doesnt work fine for me. \0 is not appended at the end of line.

try this work around

nawk -v zero='\\0' '{$0=$0zero }1' infile
nawk -v zero='0' '{$0=$0"\\"zero }1' infile

BR

Alternatively, you could use Perl if you have it:

C:\>
C:\>type f2
M:\mmarimut_v6.4.0_pit_01\java\build.xml@@\main\v6.4.0_pit_a
M:\mmarimut_v6.4.0_pit_01\ADBasicView.java@@\main\v6.4.0_pit_a
C:\>
C:\>perl -pi.bak -e "s/$/\\0/" f2
C:\>
C:\>type f2
M:\mmarimut_v6.4.0_pit_01\java\build.xml@@\main\v6.4.0_pit_a\0
M:\mmarimut_v6.4.0_pit_01\ADBasicView.java@@\main\v6.4.0_pit_a\0
C:\>
C:\>

tyler_durden

Hi ahmad.diab, Thanks very much for the help. This works great for me gawk '{$0=$0"\\\0" }1' infile.

Thanks again!!!