Jump to content

Filter a variable without awk/sed?


Recommended Posts

I want to filter a variable for slashes:

var=asdf/tzui

echo ${var#/}

Does not work, it filters only lines. So i have to find a way to filter the contents of a variable for / or and to change it in something else, p.e. a -

var=asdf/tzui

result=.........var../..-.....

echo $result

should lead into "asdf-tzui"...

Link to comment
Share on other sites

seeing as though you don't want to use sed i've come up with a dirty hack

 

var=asdf/tzui

part1=`echo $var | cut -d/ -f 1`

part2=`echo $var | cut -d/ -f 2`

result=`echo $part1-$part2`

echo $result

 

which gives asdf-tzui is this what you want??

Link to comment
Share on other sites

It's a possibility only available in BASH and Kornshell.

 

If you've got a variable like $var, you can echo the filtered variable:

 

echo ${var/...}

 

Where ... is the content to be removed. There are lot's of additional possibilities, like remove all until end/begin of string depending on a sequence.

Ex.:

var = "Hallo du Freak!"

var=`echo ${var/ Freak}`

echo $var

 

Leads to: "Hallo du!"

 

One disadvantage is that this way is by far NOT so complex like sed or awk. Only simple things can be done like this...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...