0

I am working on a wit.ai project and I want the user to be able to type a query/request in plain human readable text (e.g "Turn on the lights") and the program will convert it to a formatted version designed to be put in a curl request (e.g "Turn%20on%20the%20lights").

How would I for example turn "Stack Exchange" into "Stack%20Exchange" using tools in bash?

0

2 Answers 2

4

Not really a basic shell tool but I've used jq for the job:

$ echo -n "Stack exchange" | jq -sRr @uri
Stack%20exchange%0A

To get rid of the %0A use echo -n, naturally.

Sign up to request clarification or add additional context in comments.

2 Comments

echo -n is non-standard, so this instead: printf 'Stack exchange' | jq -sRr @uri. Or even better: jq -nRr --arg s 'Stack exchange' '$s|@uri' saving an unnecessary sub-shell.
how can we decode this ?
0
read -e a # read from the keyboard interactively into the variable `a`
sed 's/ /%20/g' <<<$a # replace spaces to "$20" in `a` with the command `sed` and print it

or

echo $a |sed 's/ /%20/g'

If you want to put it into a variable:

b=$(sed 's/ /%20/g' <<<$a)

1 Comment

Can you explain how this works? And what variables I would replace with my own

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.