Need help with switching field/column values

Hi all, I need some help on switching field/column values. For example I have a file name data.txt which contains:
a b
a b
a b
and I want to switch a and b and save it to the same file.
the file data.txt then will have:
b a
b a
b a
The problem is, well, I know how to switch field values in awk, but it's not gonna modify the file itself. I could just output the result to another file and change the file name, but now I want to modify the file data.txt directly. Is there any way to do it? And I would like to know if vi can do that. Thank you very much.

I know Perl has ways to re-write the file in-place, but I'm not too familiar with swapping fields.
Probably something like 's/(.) (.)/\2 \1/g'

Woo, that works great! Thank you so much.

If you wanted to automate the process, stick the commands into a file and have vim make the changes using the ex mode.

For example:

commandFile:
:% s/\(.\) \(.\)/\2 \1/g
:1,$ print <-- not needed, but helpful to show you the changes
:wq

datafile:
a b
a b
a b

vim -e -s < commandFile datafile

-e = ex mode
-s = silent mode

Note that the changes get written back to the datafile

You could also call "ex" directly to do this:

ex < commandFile datafile

Hi,
I think awk is easy to handle this. Try follow one.

echo  "a b" | awk '{print $2" " $1}'

if all you really have is "a b" for each line:

perl -pi.bak -e 'chomp;$_=reverse($_)."\n"' yourfile.txt

cat data.txt | rev
rev data.txt