String manipulation using ksh script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String manipulation using ksh script
# 1  
Old 03-08-2012
String manipulation using ksh script

Hi,

I need to convert string "(joe.smith" into "joe_smith"
i.e. I need to remove the leading opening brace '(' and replace the dot '.' with an under score '_'

can anyone suggest a one liner ksh script or unix command for this please
# 2  
Old 03-08-2012
In bash/ksh you can do:

Code:
$ NAME="(joe.smith"
$ NAME=${NAME#\(}
$ NAME=${NAME/./_}
$ echo $NAME
joe_smith

---------- Post updated at 11:11 AM ---------- Previous update was at 11:03 AM ----------

One lines (lol):
Code:
$ NAME="(joe.smith"; TNAME=${NAME#\(}; echo ${TNAME/./_}
joe_smith
 
$ echo '(joe.smith' | tr -d '(' | tr '.' '_'
joe_smith
 
$ echo '(joe.smith' | sed -e 's/^(//' -e 's/\./_/'
joe_smith
 
$ echo '(joe.smith' | awk '{gsub(/^\(/, ""); gsub(/\./, "_")} 1'
joe_smith


Last edited by Chubler_XL; 03-08-2012 at 09:17 PM.. Reason: Checked and fixed for ksh
# 3  
Old 03-09-2012
For smaller file size or single line..this cud be the best

echo '(joe.smith' |sed 's/^(//g'|sed 's/\./_/g'
# 4  
Old 03-09-2012
Thanks you guys .. it worked !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh String Manipulation - removing variables from within a variable

Hi. I'd like to remove all values in a string variable that also exist in a second variable. What is the appropriate approach to take here? I can use a 'For' loop and check each element and then populate a new string. But is there a cleaner, simpler way? E.g. I have the following 2 variables ... (19 Replies)
Discussion started by: user052009
19 Replies

2. Shell Programming and Scripting

Deleting part of a string : string manipulation

i have something like this... echo "teCertificateId" | awk -F'Id' '{ print $1 }' | awk -F'te' '{ print $2 }' Certifica the awk should remove 'te' only if it is present at the start of the string.. anywhere else it should ignore it. expected output is Certificate (7 Replies)
Discussion started by: vivek d r
7 Replies

3. Shell Programming and Scripting

KSH - need to write a script to abbreviate a string

Dear Members, I have to write a UNIX script (KSH) to create a 6 letter abbreviation from a given string. The string will have alphabets and underscores only. e.g. abc_pst_xyz is our string and I have to create an abbreviation which will look like 'abpsxy' or 'abcpyz' etc. Now comes the... (8 Replies)
Discussion started by: Yoodit
8 Replies

4. Shell Programming and Scripting

sed string manipulation in shell script

Hello, I have 1000 of sql queries and i need to push column value in query. e.g. SET INSERT_ID=1 INSERT INTO test (id,name) VALUES ('a'); SET INSERT_ID=2 INSERT INTO test (id,name) VALUES ('b'); SET INSERT_ID=3 INSERT INTO test (id,name) VALUES ('c'); SET INSERT_ID=4 INSERT INTO test... (12 Replies)
Discussion started by: mirfan
12 Replies

5. Shell Programming and Scripting

Eval Tricky Manipulation of Arry in KSH - Help

Hi, Could any one share the intelligence to track this problem. I have any array BT_META_36 and it prints properly with contents of array. # print "BT_META_36=${BT_META_36}" # BT_META_36=cab3,cab4:HDS:052,07A cab3,cab4:HDS:052,07A Now I have a BT_META_36 assigned to a variable.... (0 Replies)
Discussion started by: ajilesh
0 Replies

6. Shell Programming and Scripting

String format in KSH script

Hello, I am attempting to format the output of my unix ksh script. Currently looks like ... FTP TO HR : Down FTP FROM HR 4011a : Up FTP FROM HR 4019a : Down FTP FROM HR : Down Would like the status to be aligned as follows: ... (4 Replies)
Discussion started by: dvella
4 Replies

7. Shell Programming and Scripting

string manipulation in ksh

Hi all, I'm trying to extract the name of a script that is being run with a full path. i.e. if the script name is /some/where/path/script_name.ksh I'd like to extract only: script_name i know that it is possible to do so in two phases: echo "${0##*/}" will give me script_name.ksh and... (4 Replies)
Discussion started by: iceman
4 Replies

8. UNIX for Dummies Questions & Answers

String manipulation using ksh

I have a UNIX shell where: LEVEL=dev SITE=here and WHEREIAM=/tmp/$SITE/location/$LEVEL I want to echo $WHEREIAM in such a way that I get it back with all the environment variables resolved (/tmp/here/location/dev). This command will be used in a shell script. (5 Replies)
Discussion started by: zambo
5 Replies

9. AIX

Problem in ksh script ( String comparison )

hi , i am trying to compre two strings if ] or if ] when the length of var1 is small (around 300-400 char ) it works fine but when it is large (around 900-1000 chars) it fails is there any limitations for this type of comparison ??? (1 Reply)
Discussion started by: amarnath
1 Replies

10. Shell Programming and Scripting

Problem in ksh script ( String comparison )

hi , i am trying to compre two strings if ] or if ] when the length of var1 is small (around 300-400 char ) it works fine but when it is large (around 900-1000 chars) it fails is there any limitations for this type of comparison ??? (3 Replies)
Discussion started by: amarnath
3 Replies
Login or Register to Ask a Question