hi, a hard-working but horribly lost cs major here.

i'm working on a script that does global renaming using find, awk, and sed, which has wildcard inputs. i'm wondering if there's any way to strip the asterisk from the input and save it to some variable, say $str1 or just $s1.

i've been working on this problem for an hour or so, and i'm able to echo it as below:

echo $1 | sed 's/*//'

such that something like foo* would be foo and *foo would be foo.

i have been working hard on this and would appreciate any feedback at all.

Recommended Answers

All 5 Replies

Hey There,

You should be able to match a literal asterisk in sed with

echo $1 | sed 's/\*//'

or

echo $1 | sed 's/\*//g'

to get all the asterisks in the variable if there are more than one.

Please let me know if I'm misunderstanding your question. You may run into problem with the echo $1 if that variabe includes a * and the shell interprets it before you can pass it to sed through the pipe.

You can generally get around that by doing:

echo '$1'

Hope that helps :)

, Mike

howdy stranger,

thank you! that worked for stripping the asterisks.

is there a way i can get that input into sed? here's my snippet:

| sed 's/foo/bar/2'

which is part of a pipe into awk and find. so the input into the script would be something like ./my_script *foo *bar, and i'm trying to get it so that the inputs without asterisks are properly formatted and put into the above sed command (where foo* and bar* are arbitrary user inputs). is there a way to do that without resorting to variables?

and thank you once again for the way to strip asterisks. you're a lifesaver :D i've been refreshing every couple of minutes waiting for a response. thank you!

hello!

my friend has figured it out for me, thank god. here's what i meant for anyone else that needs it (since we're sharing solutions/algorithms!):

temp=`echo "$1" | sed 's:*::'`

so this one saves into temp an input with the asterisk stripped. thank you for your help! i'll work on contributing more.

HOwdy,

You're welcome. I'm glad that helped out.

The problem is probably the asterisk on the command line, since the shell interprets that before passing to the script.

You can get around it by putting this near the top of your script

#!/bin/ksh

set -o noglob

This will make it so the asterisk on the command line won't get expanded to whatever foo* expands to (e.g. foo foobar food)

Just be sure to

set +o noglob

at some point in your script, if you need to do wildcard expansion later.

Take care :)

, Mike

You should also be able to enter \*foo \*bar on the command line.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.