S
S
SOTVM2018-10-06 07:53:43
bash
SOTVM, 2018-10-06 07:53:43

How to remove special characters from a string in a request to the Google Translate API?

There is a script - it sends the selection to Google, the answer is displayed via notify-send and / or ... (it doesn’t matter)
it doesn’t work with multi-line text when I select it in the terminal, it translates only one line, I
tried adding such commands to the pipeline - it doesn’t help. CHADNT?
tr '\n' ' ' | tr '\r' ' ' | tr '\t' ' ' | tr '\v' ' '
sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/ /g'

#!/bin/bash
TRANS="$(wget -U "Mozilla/5.0" -qO - "http://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=ru&dt=t&q=$(xsel -o | sed "s/[\"'<>]//g" | tr '\n' ' ' | tr '\r' ' ' | tr '\t' ' ' | tr '\v' ' '  | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/ /g' | sed "s/,,,0]],,.*//g" )"  |  awk -F'"' '{print $2}')"

if [ "$?" -eq 0 ]; then
xcowsay -t 0 "$TRANS"
#notify-send "Перевод" "$TRANS"
#echo  "$TRANS" >> translate.txt
fi

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xotkot, 2018-10-08
@xotkot

it can be simpler using the data-urlencode key in curl:

$ Q='Hello world.
\`quote> Hi'
$ echo $Q
Hello world.
Hi
$ curl -s -H "user-agent: Mozilla/5.0" --get "https://translate.googleapis.com/translate_a/single?client=gtx&dt=t&sl=en&tl=ru" --data-urlencode "q=$Q"
[,null,"en"]

S
Saboteur, 2018-10-06
@saboteur_kiev

I suspect that the problem is in xsel -o
Perhaps he sends each line as a separate object.
Try to get the sent text into a variable first and see what's in it.
If instead of xcel -o we substitute multiline text, everything works

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question