I think you want,

 >ruby -pe 'sub(/(\S+)\s+(\S+)/,"\\2 \\1")' data.txt


Tobi Reif wrote:

> Hi
> 
> Let's say I'd want to modify each line of a file. Here's the dummy
> data:
> 
>   $ cat data.txt
>   first foo
>   second foo
>   third foo
>   $
> 
> This works:
> 
>   $ ruby -ne '$_ =~ /(\S+)\s+(\S+)/;puts "#{$2} #{$1}"' data.txt
>   foo first
>   foo second
>   foo third
>   $
> 
> I don't really need to achieve that modification. Instead I'd like to
> understand why the following version one doesn't work:
> 
>   $ ruby -pe 'sub(/(\S+)\s+(\S+)/,"#{$2} #{$1}")' data.txt
> 
>   foo first
>   foo second
>   $
> 
> TIA,
> Tobi