The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to compare a field value of a same column? gobinath Shell Programming and Scripting 5 05-28-2008 09:15 PM
replace the column values. charandevu Shell Programming and Scripting 6 04-01-2008 11:21 PM
how to read the column and print the values under that column gemini106 Shell Programming and Scripting 6 03-28-2008 04:05 AM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 06:57 AM
replace a column values with the first value in column sumeet UNIX for Advanced & Expert Users 3 02-06-2007 10:13 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 02-08-2008
Registered User
 

Join Date: Feb 2008
Posts: 4
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.
Reply With Quote
Forum Sponsor
  #2  
Old 02-08-2008
Registered User
 

Join Date: Dec 2007
Posts: 12
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'
Reply With Quote
  #3  
Old 02-10-2008
Registered User
 

Join Date: Feb 2008
Posts: 4
Quote:
Originally Posted by strike-2 View Post
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.
Reply With Quote
  #4  
Old 02-10-2008
Registered User
 

Join Date: Jan 2008
Posts: 18
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
Reply With Quote
  #5  
Old 02-10-2008
Registered User
 

Join Date: Jun 2007
Location: Beijing China
Posts: 495
awk

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

Code:
echo  "a b" | awk '{print $2" " $1}'
Reply With Quote
  #6  
Old 02-10-2008
Registered User
 

Join Date: Jan 2008
Posts: 328
if all you really have is "a b" for each line:

perl -pi.bak -e 'chomp;$_=reverse($_)."\n"' yourfile.txt
Reply With Quote
  #7  
Old 02-12-2008
Registered User
 

Join Date: Feb 2008
Posts: 6
Code:
cat data.txt | rev
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0