Hi all,
I've a file named for_test which has the following three lines in it just
as the output given by the cat command:
$ cat for_test
set base_path /home/werner/Desktop/test_awk_sub/apt-mirrorfdsfdsfds
clean
http://mirror.bjtu.edu.cn/debian
clean
http://mirror.bjtu.edu.cn/debian-multimedia
Now, I want to substitute the first line in this file with the following
content:
set base_path /path/to/for_test's/current_dir/apt-mirror
For detail, I want to the third field of the first line of this file be
the current directory of this file followed by the characters "apt-
mirror".
For the above purpose, I use the following awk script located in the same
directory as the for_test file:
$ cat sub_test.awk
baseDirForScriptSelf="$(cd "$(dirname "$0")"; pwd)"
awk -v a=$baseDirForScriptSelf '{if($1=="set" && $2=="base_path") sub(/
^.*$/,a"/apt-mirror",$3);print}' ./for_test 1<>./for_test
But, after I run the sub_test.awk, I found that the for_test file become
the following one:
$ cat for_test
set base_path /home/werner/Desktop/test_awk_sub/apt-mirror
clean
http://mirror.bjtu.edu.cn/debian
clean
http://mirror.bjtu.edu.cn/debian-multimedia
ltimedia
As you can see, the final for_test becomes four lines in it. The last
line "ltimedia" is appeared when I issue the command:
$ ./sub_test.awk
I cann't figure out why this should happen. Could you please give me
some hints on this strange thing?
Regards
--
.: Hongyi Zhao [ hongyi.zhao AT
gmail.com ] Free as in Freedom :.