Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Swapping strings in a file

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 01-20-2010
vino's Unix or Linux Image
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 12 March 2015, 5:33 AM EDT
Location: Sydney, Down Under
Posts: 2,848
Thanks: 0
Thanked 13 Times in 13 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 Unix and Linux 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 Unix and Linux 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 Unix and Linux 01-20-2010
xoops xoops is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 4 August 2015, 6:49 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 Unix and Linux 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 Unix and Linux 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 07:54 PM.