WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: I need help from SED gurus  (Read 2237 times)

Offline fangis

  • Newbie
  • *
  • Posts: 41
    • rafalis2
I need help from SED gurus
« on: March 23, 2014, 05:55:42 PM »
Hello friends,

I have searched the documentation but can't seem to make this work.
What I want to do is the following:
get the user to type a word
convert the spaces in this word to commas, and save it into a new variable

but when i use:
read var
i dont know how to make sed modify this var. Is it possible?
Thanks for the help, i appreciate it.

Offline Jason W

  • Administrator
  • Hero Member
  • *****
  • Posts: 9730
Re: I need help from SED gurus
« Reply #1 on: March 23, 2014, 06:16:43 PM »
var=`echo $var | sed 's: :,:g'`

Does that do what you need?

Offline fangis

  • Newbie
  • *
  • Posts: 41
    • rafalis2
Re: I need help from SED gurus
« Reply #2 on: March 24, 2014, 08:36:09 AM »
hi, thanks for the help.
No luck yet, i made the following script:

#!/bin/sh
echo "please type the string:"
read var
var=echo $var | sed 's::,:g'
echo "your converted string: $var"


this way gave no results, i typed, this is a test, gives:
line 5:this: not found
your converted string: this is a test

I also tried to use commas around echo, but it seems to not work
can you try the script?
thank you!
fangis

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11213
Re: I need help from SED gurus
« Reply #3 on: March 24, 2014, 09:53:20 AM »
Hi fangis
You left out the back ticks usally found on the ~ key.
Try changing this:
Code: [Select]
var=echo $var | sed 's::,:g'To this:
Code: [Select]
var=`echo $var | sed 's: :,:g'`

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: I need help from SED gurus
« Reply #4 on: March 24, 2014, 11:07:08 AM »
Or using tr:
var=`echo $VAR | tr " " ","`
or
var=$(echo $var | tr " " ",")

Offline fangis

  • Newbie
  • *
  • Posts: 41
    • rafalis2
Re: I need help from SED gurus
« Reply #5 on: March 24, 2014, 01:38:45 PM »
hey, thanks for the replies. It has worked! And I also did not know about this tr command so i will have a look. take care everyone.