Received: by 10.66.83.35 with SMTP id n3mr3472787pay.23.1350352593209; Mon, 15 Oct 2012 18:56:33 -0700 (PDT) Path: s9ni9014pbb.0!nntp.google.com!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!border4.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!border2.nntp.ams.giganews.com!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!news.panservice.it!feeds.phibee-telecom.net!zen.net.uk!dedekind.zen.co.uk!reader02.nrc01.news.zen.net.uk.POSTED!not-for-mail Newsgroups: comp.unix.shell From: Geoff Clare Subject: Re: A strange issue when using awk to substitute a field in specific line. References: User-Agent: XPN/1.2.6 (Street Spirit ; Linux) MIME-Version: 1.0 Date: Thu, 11 Oct 2012 13:33:17 +0100 Message-ID: Lines: 47 Organization: Zen Internet NNTP-Posting-Host: 7d946b44.news.zen.co.uk X-Trace: DXC=iBJOm30>2D_oS>6CUf2S_]]G;bfYi23hT=dR0\ckLKGPWeZ<[7LZNRVJE]RN?L4]9U4CJ\2i>aFoT>P83MgUBGaZo=Jo3M9UM9T X-Complaints-To: abuse@zen.co.uk Bytes: 2564 X-Received-Bytes: 2762 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Hongyi Zhao wrote: > $ 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? Because you are using 1<>file to redirect standard output, the shell does not truncate the file before executing awk. When awk writes to the file it is overwriting existing data. Your transformation shortens the data, and therefore there is some of the old data left at the end when awk has finished writing the new data. The 1<>file trick is neat, but it should only really be used when you are doing a one-to-one transformation. It's also only safe if the transformation is idempotent (i.e. repeating the change does not alter the result). This is so that if awk is killed part way through, you can just restart it from the beginning. An example of a safe usage is: $ cat for_test abcabc $ awk '{ gsub(/b/, "B"); print }' for_test 1<>for_test $ cat for_test aBcaBc -- Geoff Clare