stripping certain characters in at the middle of a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting stripping certain characters in at the middle of a string
# 1  
Old 05-12-2008
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 thanksSmilie
# 2  
Old 05-12-2008
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.
# 3  
Old 05-12-2008
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.Smilie
# 4  
Old 05-13-2008
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).
# 5  
Old 05-13-2008
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Stripping unwanted characters in field

I wrote myself a small little shell script to clean up a file I have issues with. In particular, I am stripping down a fully qualified host/domain name to just the hostname itself. The script works, but from a performance standpoint, it's not very fast and I will be working with large data sets. ... (4 Replies)
Discussion started by: dagamier
4 Replies

2. Shell Programming and Scripting

Stripping characters from a file and reformatting according to another one

Dear experts, my problem is pretty tricky. I want to change a file (see attached input.txt), according to another file (help.txt). The output that is desired is in output.txt. The example is attached. Note that -dashes should not be treated specially, they are considered normal characters,... (2 Replies)
Discussion started by: TheTransporter
2 Replies

3. Shell Programming and Scripting

stripping out digits from a string with sed

i want to parse a string and only display the digits in that string... How would i accomplish this with sed command. For example. input string: " 033434343 dafasdf" output string: 03343434 Thanks (2 Replies)
Discussion started by: timmylita
2 Replies

4. Shell Programming and Scripting

Stripping characters from a variable

I'm using a shell script to get user input with this command: read UserInput I would then like to take the "UserInput" variable and strip out all of the following characters, regardless of where they appear in the variable or how many occurrences there are: \/":|<>+=;,?*@ I'm not sure... (5 Replies)
Discussion started by: nrogers64
5 Replies

5. UNIX for Dummies Questions & Answers

sed replacing in vi, / characters in the middle

I tried to replace the following in vi: old: 'e/thesis/pp/zones/zones' new: 'd/so162/fix/pp' For that, I used: :%s/e/thesis/pp/zones/zones/d/so162/fix/pp/g but doesn't work, a trailing character error message appeared. How can I get it? Thanks in advance (3 Replies)
Discussion started by: Gery
3 Replies

6. Shell Programming and Scripting

Bash script - stripping away characters that can't be used in filenames

I want to create a temp file which is named based on a search string. The search string may contain spaces or characters that aren't supposed to be used in filenames so I want to strip those out. My thought was to use 'tr' with but the result is the opposite of what I want: $ echo "test... (5 Replies)
Discussion started by: mglenney
5 Replies

7. Shell Programming and Scripting

Stripping the spaces in a string variable

Hi , i have to strip the spaces in the string which has the following value ABC DEF i want this to appear like this ABC DEF is there any spilt method? please help.... Thanks (3 Replies)
Discussion started by: rag84dec
3 Replies

8. Shell Programming and Scripting

stripping leftmost characters from string

Hi there, if i have some strings ie test_324423 test_242332 test_767667 but I only want the number part (the bolded bit) how do I strip the leftmost 5 characters from the output so that i will have just 324423 242332 767667 any help would be greatly appreciated Gary (5 Replies)
Discussion started by: hcclnoodles
5 Replies

9. UNIX for Dummies Questions & Answers

Stripping a portion of string from behind!!!

Hi, How to strip a portion of a file name from behind...Say for Eg..i have a file name like aaaaa.bbbbb.Mar-17-2007 i want to remove .Mar-17-2007...is there a one line command which can give this output... Thanks Kumar (5 Replies)
Discussion started by: kumarsaravana_s
5 Replies

10. Programming

how to insert and delete characters in the middle of file

I have a problem that I want to insert and delete some chars in the middle of a file. fopen() and fdopen() just allow to append at the end. Is there any simple method or existing library that allow these actions? Thanks in advance.:confused: (7 Replies)
Discussion started by: ivancheung
7 Replies
Login or Register to Ask a Question