From: Hongyi Zhao on 26 Nov 2009 00:46 On Wed, 25 Nov 2009 22:41:04 -0600, Ed Morton <mortonspam(a)gmail.com> wrote: >OK, so you don't actually want to swap anything, you want to find "line_b" and >move it to earlier in the file, specifically right after "line_a", right? try this: > >tac file | awk '/line_b/{b=$0 RS; next} /line_a/{printf "%s",b} 1' | tac Let me give a minimal example: $ cat test_swap2lines.txt some_others1 line_b some_others2 line_a some_others3 I want to obtain the following result: some_others1 some_others2 line_a line_b some_others3 But, your above code will give something like this: $ tac test_swap2lines.txt | awk '/line_b/{b=$0 RS; next} /line_a/{printf "%s",b } 1' | tac some_others1 some_others2 line_a some_others3 In the result given by your code, the line_b is removed from the result, which is not the result I want. Best regards. -- ..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
From: w_a_x_man on 26 Nov 2009 04:47 On Nov 25, 11:46 pm, Hongyi Zhao <hongyi.z...(a)gmail.com> wrote: > On Wed, 25 Nov 2009 22:41:04 -0600, Ed Morton <mortons...(a)gmail.com> > wrote: > > >OK, so you don't actually want to swap anything, you want to find "line_b" and > >move it to earlier in the file, specifically right after "line_a", right? try this: > > >tac file | awk '/line_b/{b=$0 RS; next} /line_a/{printf "%s",b} 1' | tac > > Let me give a minimal example: > > $ cat test_swap2lines.txt > some_others1 > line_b > some_others2 > line_a > some_others3 > > I want to obtain the following result: > > some_others1 > some_others2 > line_a > line_b > some_others3 > > But, your above code will give something like this: > > $ tac test_swap2lines.txt | awk '/line_b/{b=$0 RS; next} > /line_a/{printf "%s",b} 1' | tac > > some_others1 > some_others2 > line_a > some_others3 > > In the result given by your code, the line_b is removed from the > result, which is not the result I want. Using Ruby: # Read the file. gets(nil) # Remove Line B. sub!( /.*line_b.*\n/, "" ) # Remember Line B. b = $& # Insert Line B after Line A. puts sub( /line_a.*\n/, '\&' + b )
From: Hongyi Zhao on 26 Nov 2009 07:25 On Thu, 26 Nov 2009 01:47:17 -0800 (PST), w_a_x_man <w_a_x_man(a)yahoo.com> wrote: >Using Ruby: > ># Read the file. >gets(nil) ># Remove Line B. >sub!( /.*line_b.*\n/, "" ) ># Remember Line B. >b = $& ># Insert Line B after Line A. >puts sub( /line_a.*\n/, '\&' + b ) Very good, thanks a lot, you let me know a succinct and powerful tool. Best regards. -- ..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
From: Hongyi Zhao on 26 Nov 2009 07:37 On Thu, 26 Nov 2009 01:47:17 -0800 (PST), w_a_x_man <w_a_x_man(a)yahoo.com> wrote: >Using Ruby: > ># Read the file. >gets(nil) ># Remove Line B. >sub!( /.*line_b.*\n/, "" ) ># Remember Line B. >b = $& ># Insert Line B after Line A. >puts sub( /line_a.*\n/, '\&' + b ) Is it possible to edit the input file in place by uisng ruby, i.e., just like the -i OPTION for sed? Best regards. -- ..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
From: w_a_x_man on 26 Nov 2009 16:37
On Nov 26, 6:37 am, Hongyi Zhao <hongyi.z...(a)gmail.com> wrote: > On Thu, 26 Nov 2009 01:47:17 -0800 (PST), w_a_x_man > > <w_a_x_...(a)yahoo.com> wrote: > >Using Ruby: > > ># Read the file. > >gets(nil) > ># Remove Line B. > >sub!( /.*line_b.*\n/, "" ) > ># Remember Line B. > >b = $& > ># Insert Line B after Line A. > >puts sub( /line_a.*\n/, '\&' + b ) > > Is it possible to edit the input file in place by uisng ruby, i.e., > just like the -i OPTION for sed? Yes. ruby -i.bak -pe'gsub( "c", "x" )' data This makes a backup file named "data.bak" and replaces each "c" with "x". |