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.

stripping certain characters in at the middle of a string

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-12-2008
mcoblefias mcoblefias is offline
Registered User
 
Join Date: May 2008
Last Activity: 4 January 2013, 9:43 PM EST
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Error stripping certain characters in at the middle of a string

I am trying to strip out certain characters from a string on both (left & right) sides. For example, line=see@hear|touch, i only want to echo the "hear" part. Well i have tried this approach:

line=see@hear|touch
templine=${line#*@} #removed "see@"
echo ${templine%%\|*} #removed "|touch"
hear

...apparently it worked but i want to do this in only one line. Can anyone suggest a way on how to do this?

Another question, what if i added some more characters like this:

line=see@hear|touch|smell

...now i wanted to separate each and every one of them dynamically* like this: (*the values for templine2, 3 & 4 may vary)

templine1=see
templine2=hear
templine3=touch
templine4=smell

...can anyone also suggest on this one?

Many thanksLinux
Sponsored Links
    #2  
Old Unix and Linux 05-12-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 10 Times in 8 Posts
Really not at all clear what you are getting at, but try this.


Code:
vnix$ IFS='|@'
vnix$ read stuff into variables
see@hear|touch@smell
vnix$ echo $stuff
see
vnix$ echo $into
hear
vnix$ echo $variables
touch smell

The | is not a very suitable separator, otherwise you could also use set -- to split this into multiple tokens.
Sponsored Links
    #3  
Old Unix and Linux 05-12-2008
mcoblefias mcoblefias is offline
Registered User
 
Join Date: May 2008
Last Activity: 4 January 2013, 9:43 PM EST
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
stripping certain characters in at the middle of a string

Sorry for that. Let me clarify.
First, i want to get the word "hear" from this string: see@hear|touch
I tried stripping first the left part ("see@") then the right part ("|touch") leaving me with only what i wanted... "hear" but, it took me a couple of lines of codes to do this. Now are there any ways to do it in only a single line of code?

Second, I want to separate each word in the string and put them into separate variables.

from
see@hear|touch|smell

to
string1=see
string2=hear
string3=touch
string4=smell

*removed separators @ and |. btw, i have to use | as a separator as it was the one instructed for me to use.

...can you suggest anything on how to do this with minimal lines of code?

Hope i made it clear enough.Linux
    #4  
Old Unix and Linux 05-13-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 10 Times in 8 Posts
It depends on where you get the input string from and what the separators are. If | is in the set it's tricker, but in general, to split on a set of characters, you can use set -- with IFS set to the token splitting characters. That's what I continue to suggest.


Code:
vnix$ IFS=@
vnix$ set -- `echo see@hear@touch@smell`
vnix$ echo $#
4
vnix$ echo $1
see
vnix$ echo $4
smell
vnix$ echo "$@"
see hear touch smell

I don't know how to make it any clearer than that. Now "see" is in $1, "hear" is in $2, "touch", in $3, and "smell" in $4. I think that satisfies your requirement.

If you are reading an input string from the terminal then you can add | to the set of splitting characters no problem, but you can't use it directly in a script because it's already a command separator (for setting up pipelines, no less).
Sponsored Links
    #5  
Old Unix and Linux 05-13-2008
fpmurphy's Unix or Linux Image
fpmurphy fpmurphy is online now Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 22 May 2015, 4:08 AM EDT
Location: /dev/ph
Posts: 4,923
Thanks: 68
Thanked 455 Times in 422 Posts
The following works in ksh93. Have not checked any other shell

Code:
$ F="see@hear|touch|smell"
$ IFS='[@|]'
$ echo $F
see hear touch smell
$ set -- $F
$ echo $1
see
$ echo $2
hear
$ echo $3
touch
$ echo $4
smell
$ echo $@
see hear touch smell

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
Stripping characters from a file and reformatting according to another one TheTransporter Shell Programming and Scripting 2 07-21-2011 01:13 PM
Stripping characters from a variable nrogers64 Shell Programming and Scripting 5 05-27-2010 04:17 PM
sed replacing in vi, / characters in the middle Gery UNIX for Dummies Questions & Answers 3 03-08-2010 10:34 AM
Bash script - stripping away characters that can't be used in filenames mglenney Shell Programming and Scripting 5 06-04-2009 02:45 PM
stripping leftmost characters from string hcclnoodles Shell Programming and Scripting 5 11-02-2007 03:52 AM



All times are GMT -4. The time now is 04:29 AM.