From: Hongyi Zhao on 27 Nov 2009 21:45 On Fri, 27 Nov 2009 23:41:18 +0800, WANG Cong <xiyou.wangcong(a)gmail.com> wrote: >awk 'BEGIN{a=b=i=0}/line_a/{a=i;} /line_b/{b=i;} {l[i++]=$0;} END{t=l[b];for(i=0;i<NR;i++){if(i!=b)print l[i];if(i==a)print t;}}' your_file This works smooth, but I want to do the in place/inline edit on the file. does awk support in place/inline edit just sed's '-i' switch? Best regards. -- ..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
From: Janis Papanagnou on 27 Nov 2009 22:24 Hongyi Zhao wrote: > On Fri, 27 Nov 2009 23:41:18 +0800, WANG Cong > <xiyou.wangcong(a)gmail.com> wrote: > >> awk 'BEGIN{a=b=i=0}/line_a/{a=i;} /line_b/{b=i;} {l[i++]=$0;} END{t=l[b];for(i=0;i<NR;i++){if(i!=b)print l[i];if(i==a)print t;}}' your_file > > This works smooth, but I want to do the in place/inline edit on the > file. does awk support in place/inline edit just sed's '-i' switch? It's rarely an inline edit what sed does; you just don't see the temporary file. No, awk has no -i option to hide the temporary file from you. But you can put the awk call in a shell script if you don't want to see the temporary. Janis > > Best regards.
From: WANG Cong on 28 Nov 2009 09:44 On 11/28/09 02:01, Hongyi Zhao <hongyi.zhao(a)gmail.com> wrote: > On Fri, 27 Nov 2009 23:42:27 +0800, WANG Cong > <xiyou.wangcong(a)gmail.com> wrote: > >>'-i' without anything, the same as 'sed'. > > Does the gets(nil) statement work with '-i' switch? In my case, I > always meet the error like this: > > test.rb:9:in `gets': Can't do inplace edit without backup (fatal) > from test.rb:9 > > Why? Are you using it on Window$? I have never seen such thing on Linux. If so, you have to provide the backup file for '-i'. -- Live like a child, think like the god.
From: Hongyi Zhao on 28 Nov 2009 21:25 On Sat, 28 Nov 2009 14:44:40 +0000, WANG Cong <xiyou.wangcong(a)gmail.com> wrote: >Are you using it on Window$? I have never seen such thing on Linux. >If so, you have to provide the backup file for '-i'. Cygwin in my case. Best regards. -- ..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
From: Michael Paoli on 30 Nov 2009 02:21
On Nov 24, 1:34 am, Hongyi Zhao <hongyi.zhao(a)gmail.com> wrote: > I want to swap the appearance order of two lines in a file if needed. > For detail, please see the following minimal example: > > ... > here_comes_line_b > ... > here_comes_line_a > ... > > Suppose the following should be the final result I want: > > ... > here_comes_line_a > ... > here_comes_line_b > ... Perhaps not what's desired, but meets the "specification" (and a bit more). It watches for a (here_comes_line_a) and b (here_comes_line_b) lines. If a b line is the first a/b line preceeding an a line, it will swap them, unless the first a/b line preceeding that b line is an a line. E.g. an aba sequence wouldn't change to aab, but a bba sequence would change to bab. sed -ne ' /^here_comes_line_[ab]$/!{ p d } /^here_comes_line_a$/{ :a ${ p q } N /\nhere_comes_line_b$/{ p d } /\nhere_comes_line_a$/{ :l /\n/P s/^[^\n]*\n// tl } ba } /^here_comes_line_b$/{ :b ${ p q } N /\nhere_comes_line_a$/{ s/^here_comes_line_b\nhere_comes_line_a$/here_comes_line_a \nhere_comes_line_b/ s/^here_comes_line_b\n\(.*\)\nhere_comes_line_a$/here_comes_line_a\n \1\nhere_comes_line_b/ p d } /\nhere_comes_line_b$/{ :m /\n/P s/^[^\n]*\n// tm } bb } ' |