Deleting part of a string : string manipulation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting part of a string : string manipulation
# 1  
Old 04-20-2012
Deleting part of a string : string manipulation

i have something like this...

Code:
 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
Code:
Certificate

# 2  
Old 04-20-2012
Without external programs (fast)
Code:
var=teCertificateId
var2=${var#te}
var3=${var2%Id}
echo "$var3"

With external programs:
Code:
echo "$var" | sed 's/^te//;s/Id$//'

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 3  
Old 04-20-2012
Hi


Code:
$  echo "teCertificateId" | awk -F'Id' '{sub(/^te/,"",$1); print $1 }'
Certificate

Guru,
This User Gave Thanks to guruprasadpr For This Post:
# 4  
Old 04-20-2012
thanks a lot :-)
# 5  
Old 04-20-2012
And a third way:
Code:
echo "teCertificateId"| sed 's/^te//; s/Id$//'

This User Gave Thanks to zaxxon For This Post:
# 6  
Old 04-20-2012
Code:
 
echo "teCertificateId" | nawk '{gsub("^te|Id$","",$0);print}' 
Certificate

# 7  
Old 04-20-2012
A neater way within ksh is to slice the variable and it doesn't spawn a new process. If you know that "te" is to be removed from the begginning and "Id" from the end then it's simply like Scrutinizer wrote:-
Code:
var="teCertificateId"
var2="${var#te}"
echo "${var2%Id}"

however if you just want to trim any two leawding and trailing characters you need to use the wildcard ?:-
Code:
var=teCertificateId
var2="{var##??}"
echo "{var2%%??}"


Does this help?




Robin
Liverpool/Blackburn
UK
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

String manipulation

i have a string that am looking to extract all characters following 3 consecutiv numbers. Example my string is J1705PEAN038TDMN, i need to get TDMN My string can have multiple 3 consecutive numbers, i need what follows last occurance (9 Replies)
Discussion started by: gigagigosu
9 Replies

2. Shell Programming and Scripting

String manipulation

Hello Could you help with small script: How to split string X1 into 3 string String X1 can have 1 or many strings X1='A1:B1:C1:D1:A2:B2:C2:D2:A3:B3:C3:D3' This is output which I want to have: Z1='A1:B1:C1:D1' Z2='A2:B2:C2:D2' Z3='A3:B3:C3:D3' (5 Replies)
Discussion started by: vikus
5 Replies

3. Shell Programming and Scripting

String manipulation

I want to do the next "I don't want to go school because I'm sick today." I want to join these two line but only when the first line is not more than 20 characters and ended whit nothing or a comma and the second line not more than 15. The 20 and the 15 can be change in the script. I know... (10 Replies)
Discussion started by: thailand
10 Replies

4. Homework & Coursework Questions

String Manipulation

Write a shell program to display the position of the right - most character in a given input string. Example : Input : RAHUL Output : L is in the 5th position also tell me how to count length of string and how to find the position of specific character in left most side. Homework... (0 Replies)
Discussion started by: shashwat2691
0 Replies

5. Shell Programming and Scripting

String Manipulation

Write a shell program to display the position of the right - most character in a given input string. Example : Input : RAHUL Output : L is in the 5th position also tell me how to count length of string and how to find the position of specific character in left most side. (1 Reply)
Discussion started by: shashwat2691
1 Replies

6. Shell Programming and Scripting

String manipulation

Hi, I have the string like this ". Start : 06:53:11 - MON JUL 05, 2010" I need to print the part "06:53:11 - MON JUL 05, 2010" How i can do this? Thanks, Lenova (2 Replies)
Discussion started by: lenova2010
2 Replies

7. Shell Programming and Scripting

String Manipulation

How u convert string "hi pravin how are you?" to "Hi Pravin How Are You?" (4 Replies)
Discussion started by: proactiveaditya
4 Replies

8. Shell Programming and Scripting

Deleting part of a string enclosed in brackets

I use otool on OS X to figure out the shared libraries that a binary uses. I run this command: otool -L /Applications/Vidnik\ 0.13.0/Vidnik.app/Contents/MacOS/Vidnik And it returns an output similar to this: /Applications/Vidnik 0.13.0/Vidnik.app/Contents/MacOS/Vidnik:... (10 Replies)
Discussion started by: pcwiz
10 Replies

9. Shell Programming and Scripting

String Manipulation Help

Hey Guys, Right i know how to alter a word to begin with a capital letter, i know how to remove unwanted characters and replace them with the relevant character however i don't now if there is a way to do them all in one line. Code: echo -n ${string:0:1} | tr a-z A-Z #convert first letter... (4 Replies)
Discussion started by: shadow0001
4 Replies

10. Shell Programming and Scripting

string manipulation

Hello, I have a korn shell string variable str1 = "A,B,Z" I would like to create another korn shell string variable str2 = "letter = 'A' or letter = 'B' or letter = 'Z' " Please help! Thanks in advance an UNIX newbie! (13 Replies)
Discussion started by: hai1973
13 Replies
Login or Register to Ask a Question