Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Swap two characters in a string

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-14-2017
Soham Soham is offline
Registered User
 
Join Date: Sep 2007
Last Activity: 21 September 2017, 2:16 PM EDT
Location: UNIX
Posts: 56
Thanks: 18
Thanked 1 Time in 1 Post
Swap two characters in a string

I want to swap hyphen (-) and underscore (_) in a string. In fact only one will be present. So if it is hyphen I want to make it underscore and if it is underscore I want to make it hyphen.

I can write a long code like below. Is there any easy way?


Code:
#!/bin/ksh
 
typeset -i exists
str="ag-sd"
 
new_str=$str
 
exists=`print $new_str | grep "\-" | wc -l`
if [[ $exists -eq 1 ]]
then
   str=`print $new_str | sed 's/\-/\_/g'`
fi
 
exists=`print $new_str | grep "\_" | wc -l`
if [[ $exists -eq 1 ]]
then
   str=`print $new_str | sed 's/\_/\-/g'`
fi
 
print "Old string $new_str"
print "New string $str"

Sponsored Links
    #2  
Old Unix and Linux 07-14-2017
drl's Unix or Linux Image
drl drl is offline Forum Advisor  
Registered Voter
 
Join Date: Apr 2007
Last Activity: 23 September 2017, 6:28 PM EDT
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,169
Thanks: 220
Thanked 400 Times in 345 Posts
Hi.

This looks like it does what you wish:

Code:
$ cat z8; tr '_-' '-_' < z8
abc-def_hij-klm_nop-qrs_tuv-wxy_z
abc_def-hij_klm-nop_qrs-tuv_wxy-z

and

Code:
$ echo "$s" ; echo "$s" | tr '_-' '-_'
abc-def_hij-klm_nop-qrs_tuv-wxy_z
abc_def-hij_klm-nop_qrs-tuv_wxy-z

For a system like:

Code:
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.8 (jessie) 
tr (GNU coreutils) 8.23

Best wishes ... cheers, drl
The Following User Says Thank You to drl For This Useful Post:
Soham (07-17-2017)
Sponsored Links
    #3  
Old Unix and Linux 08-17-2017
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 23 September 2017, 5:20 PM EDT
Location: Simplicity
Posts: 3,748
Thanks: 306
Thanked 1,257 Times in 1,136 Posts
In sed you can use the y command

Code:
str='abc-def_hij-klm_nop-qrs_tuv-wxy_z'
newstr=`printf "%s\n" "$str" | sed 'y/_-/-_/'`
printf "%s\n" "$str" "$newstr"

    #4  
Old Unix and Linux 08-17-2017
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 23 September 2017, 1:33 PM EDT
Location: Amsterdam
Posts: 11,535
Thanks: 500
Thanked 3,336 Times in 2,944 Posts
With awk:

Code:
$ echo a_b-cc_dd_ee-ff | awk '{for(i=1; i<=NF; i++) gsub(OFS,FS,$i)}1'  FS=- OFS=_
a-b_cc-dd-ee_ff



--
ksh93:

Code:
$ echo "$var"
a_b-cc_dd_ee-ff
$ var2=${var//-/+} var2=${var2//_/-} var2=${var2//+/_}
$ echo "$var2"
a-b_cc-dd-ee_ff


Last edited by Scrutinizer; 08-17-2017 at 04:58 PM..
Sponsored Links
Reply

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
remove characters from string based on occurrence of a string victor369 Shell Programming and Scripting 5 02-03-2011 08:37 PM
Swap different size string rupeshkp728 Programming 2 06-29-2010 08:17 AM
Swap device file and swap sapce lamoul HP-UX 6 03-04-2010 08:14 AM
get certain characters in a string jimmy_y Shell Programming and Scripting 4 01-28-2010 07:44 PM
Add string after another string with special characters heliode Shell Programming and Scripting 2 03-21-2008 08:06 AM



All times are GMT -4. The time now is 09:34 PM.