LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 08-11-2008, 11:08 AM   #1
Sherlock
Member
 
Registered: Mar 2004
Location: India
Distribution: RedHat Linux 8
Posts: 60

Rep: Reputation: 15
Stripping a string to create a new string


Hi All,

I have a string

temp="TST_NS_TABL_PS";

I want to create a new string from this stripping "_TBL".

temp1 = "TST_NS_PS";

Hw can i do it...someone suggested sed...but i dont hv much idea abt it..

Iam using Solaris OS

Thanks in advance.

Regards
 
Old 08-11-2008, 11:11 AM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
stripped=${temp/_TABL/}
 
Old 08-11-2008, 11:16 AM   #3
Sherlock
Member
 
Registered: Mar 2004
Location: India
Distribution: RedHat Linux 8
Posts: 60

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Mr. C. View Post
stripped=${temp/_TABL/}
hw about the sed command?


And the execution says

temp=${temp/_TABL/}: bad substitution

Last edited by Sherlock; 08-11-2008 at 11:18 AM.
 
Old 08-11-2008, 11:27 AM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
For the future, indicate what shell you are using.

Using sed:

stripped=$(echo $temp | sed 's/_TABL//')
 
Old 08-11-2008, 11:33 AM   #5
Sherlock
Member
 
Registered: Mar 2004
Location: India
Distribution: RedHat Linux 8
Posts: 60

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Mr. C. View Post
For the future, indicate what shell you are using.

Using sed:

stripped=$(echo $temp | sed 's/_TABL//')

Am using bash shell...on Solaris 8.0
 
Old 08-11-2008, 12:24 PM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Using sed will work, and is probably pretty portable to different shells. However, it is not never efficient, so if you are doing the operation many times, the time taken can quickly add up.

Using parameter expansion modifiers is much quicker. See the Parameter Expansion section of the bash manual page for a list of possible expansions which work in bash. A few of these are bash-specific. If you use a different shell, check the relevant part of the manual page for your shell.

Code:
#!/bin/bash
temp1="TST_NS_TABL_PS"
temp2="${temp1/_TABL/}"

echo "temp1=$temp1"
echo "temp2=$temp2"
 
Old 08-13-2008, 02:49 AM   #7
Sherlock
Member
 
Registered: Mar 2004
Location: India
Distribution: RedHat Linux 8
Posts: 60

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by matthewg42 View Post
Using sed will work, and is probably pretty portable to different shells. However, it is not never efficient, so if you are doing the operation many times, the time taken can quickly add up.

Using parameter expansion modifiers is much quicker. See the Parameter Expansion section of the bash manual page for a list of possible expansions which work in bash. A few of these are bash-specific. If you use a different shell, check the relevant part of the manual page for your shell.

Code:
#!/bin/bash
temp1="TST_NS_TABL_PS"
temp2="${temp1/_TABL/}"

echo "temp1=$temp1"
echo "temp2=$temp2"


Thanks All!!!!!!!!!!

This worked

Code:
stripped=$(echo $temp | sed 's/_TABL//')
 
  


Reply

Tags
sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to change String using sed with a string \/home\/user\/Desktop icecoolcorey Programming 10 06-12-2008 11:32 PM
Rename files by stripping text from either desired string? varney Linux - General 2 02-21-2008 10:04 PM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM
Rewrite rule with query string in the pattern string basahkuyup Linux - Newbie 2 10-17-2006 02:06 AM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM


All times are GMT -5. The time now is 08:30 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration