Swapping strings in a file | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Swapping strings in a file

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-20-2010
tej_89 tej_89 is offline
Registered User
 
Join Date: Jan 2010
Last Activity: 20 January 2010, 11:20 PM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Swapping strings in a file

Hi,

How to swap two strings in a file ?
Ex: "ABC" to be swapped with "XYZ"
"ABC" and "XYZ" donot occur in a same line .
String has ""

Regards
Tej
Sponsored Links
    #2  
Old 01-20-2010
vino's Avatar
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 9 April 2013, 8:19 AM EDT
Location: Sydney, Down Under
Posts: 2,847
Thanks: 0
Thanked 12 Times in 12 Posts
Quote:
Originally Posted by tej_89 View Post
Hi,

How to swap two strings in a file ?
Ex: "ABC" to be swapped with "XYZ"
"ABC" and "XYZ" donot occur in a same line .
String has ""

Regards
Tej
Usually you could use sed or perl. If you have GNU sed which has the -i flag, you could use sed. Else perl is good.

Code:
sed -i -e "s/ABC/XYZ/g"
perl -p -i -e "s/ABC/XYz/g"

Sponsored Links
    #3  
Old 01-20-2010
thegeek thegeek is offline
Read Only
 
Join Date: Apr 2009
Last Activity: 30 July 2012, 5:38 AM EDT
Location: /usr/bin/vim
Posts: 946
Thanks: 13
Thanked 38 Times in 36 Posts
Quote:
Originally Posted by tej_89 View Post
Hi,

How to swap two strings in a file ?
Ex: "ABC" to be swapped with "XYZ"
"ABC" and "XYZ" donot occur in a same line .
String has ""

Regards
Tej
Give an example atleast ...
    #4  
Old 01-20-2010
tej_89 tej_89 is offline
Registered User
 
Join Date: Jan 2010
Last Activity: 20 January 2010, 11:20 PM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Example is ,
I have a file test.txt, in which I need to swap "ABC" with "XYZ". I cannot use replace because I still have to retain both the strings in the same file.

File looks like this:

Code:
"DEF", "678", "ABC", "GHI", "KLM", ...."123",
"345", XYZ", "MNO", "PQR",

I need to swap "ABC" with "XYZ"

Regards
Tej

---------- Post updated at 04:12 AM ---------- Previous update was at 03:48 AM ----------

I tried using perl -p -i -e "s/ABC/XYz/g" test.txt in the command line. Command is replacing but not swapping these two strings

Last edited by Franklin52; 01-20-2010 at 04:00 AM.. Reason: Please use code tags!
Sponsored Links
    #5  
Old 01-20-2010
xoops xoops is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 29 March 2012, 3:19 AM EDT
Posts: 124
Thanks: 0
Thanked 7 Times in 7 Posts
try this


Code:
 
$ cat inf
"DEF", "678", "ABC", "GHI", "KLM", ...."123",
"345", XYZ", "MNO", "PQR",

$ cat inf | sed 's/ABC/TMP/g;s/XYZ/ABC/g;'| sed 's/TMP/XYZ/g'
"DEF", "678", "XYZ", "GHI", "KLM", ...."123",
"345", ABC", "MNO", "PQR",

Sponsored Links
    #6  
Old 01-20-2010
tej_89 tej_89 is offline
Registered User
 
Join Date: Jan 2010
Last Activity: 20 January 2010, 11:20 PM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
I tried using this command, swapping is still not happening.
File is unchanged
Sponsored Links
    #7  
Old 01-20-2010
thegeek thegeek is offline
Read Only
 
Join Date: Apr 2009
Last Activity: 30 July 2012, 5:38 AM EDT
Location: /usr/bin/vim
Posts: 946
Thanks: 13
Thanked 38 Times in 36 Posts
a simple & ugly method of doing it, which may only work for the given example.


Code:
18:18:33 : tmp :cat > t
"DEF", "678", "ABC", "GHI", "KLM", ...."123",
"345", XYZ", "MNO", "PQR",
18:18:47 : tmp :cat t
"DEF", "678", "ABC", "GHI", "KLM", ...."123",
"345", XYZ", "MNO", "PQR",
18:18:49 : tmp :perl -i t.pl t
18:18:53 : tmp :cat t
"DEF", "678", "XYZ", "GHI", "KLM", ...."123",
"345", ABC", "MNO", "PQR",
18:18:55 : tmp :cat t.pl
undef $/;

$_=<>;

s/(ABC)(.*)(XYZ)/\3\2\1/s;
print;

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Swapping of fields in a file ksailesh Shell Programming and Scripting 3 11-17-2009 10:11 AM
Swapping in VI editor Jahn UNIX for Dummies Questions & Answers 1 06-24-2009 12:18 PM
Swapping Esaia SuSE 3 06-11-2006 04:09 PM
Paging and Swapping ST2000 Filesystems, Disks and Memory 2 04-30-2004 02:47 PM
Swapping questions 98_1LE UNIX for Dummies Questions & Answers 1 08-07-2001 11:43 AM



All times are GMT -4. The time now is 05:46 AM.