Shell [[: not found

hello, just trying to do this :
Code:
text="$($1)"

if [[ $text == *"-\n"* ]]; then
        text="$(cat $text | sed 's/-//g' | tr '\n' ' ')"
elif [[ $text == *"\n"* ]]; then
        text="$(cat $texgt | tr '\n' ' ')"
fi

But [[]] not valid in /bin/sh, it's from bash

How to do the same but in shell?
 
sed uses regular expressions to "check" the string (the part between /-/). I believe the end of a line is matched with $, so use /-$/ instead.
 
But still, how to check that the string actually has the '-' char followed by "\n" (newline char).
Re-read what SirDice wrote. I often have to re-read a short sentence several times before an explanation actually starts making sense to me. Sometimes, a small detail did not register with me -and turned out to be THE difference between success and failure. An incredibly common pitfall when doing anything computer-related, it got me more times than I care to remember.
 
Granted, regexp is quite a daunting subject. Especially if you're not used to using them. But the purpose of a regular expression is so you only detect (and optionally change) very specific things. So you don't need to check this first, if the regexp is correct it will only change what needs to be changed and nothing else. The regular expression already does all this checking for you.
 
But the current regexp he tried (/-/) would change ALL the occurrences of -, not only the one at the end of the line. That's why he asked i guess.
Of course, the proper "test" would be to enhance the regex like i pointed out in an earlier post.
 
Sure. You can probably fold that tr(1) into the same sed(1) regexp. Kill two birds with one stone.

Code:
$ cat mytext.txt
some line of text
other line of text-
more text-
I'm running out of ideas to write
$ cat mytext.txt | sed -e 's/-$//;H;1h;$!d;x;y/\n/ /'
some line of text other line of text more text I'm running out of ideas to write
Took me a bit longer as it's tricky to actually remove that newline, it's easy to detect but not to replace. sed(1) works line by line and will just add that newline again after printing the changed text.
 
Oh, and I had to this with sh(1) interactively. tcsh(1) will just "eat" that !d and interpret it as a history search command. Regardless if you single or double quote the string.
Code:
dice@williscorto:~/test % cat mytext.txt | sed -e 's/-$//;H;1h;$!d;x;y/\n/ /'
cat mytext.txt | sed -e 's/-$//;H;1h;$df -h;x;y/\n/ /'
sed: 1: "s/-$//;H;1h;$df -h;x;y/ ...": extra characters at the end of d command
Notice how that $!d got translated to $df -h :eek:
 
Oh, and I had to this with sh(1) interactively. tcsh(1) will just "eat" that !d and interpret it as a history search command. Regardless if you single or double quote the string.
Code:
dice@williscorto:~/test % cat mytext.txt | sed -e 's/-$//;H;1h;$!d;x;y/\n/ /'
cat mytext.txt | sed -e 's/-$//;H;1h;$df -h;x;y/\n/ /'
sed: 1: "s/-$//;H;1h;$df -h;x;y/ ...": extra characters at the end of d command
Notice how that $!d got translated to $df -h :eek:
Nice code. I generally prefer to keep it simple, clean, and reusable to solve a specific issue, especially when trying to teach someone.
 
Found this piece:
sed 'N;s/-\n//'
it works only if the string is such:
rugged- ly
if the string is without "-\n" then the output is nothing.
Looking into man of sed and found nothing about this N;
Still looking for this "cut the hyphenation if it exists in string"
 
The solution was found with alternative search system. Google hates perl?)

cat text | perl -pe 's/-\n//g'
 
here is the whole script notitrance.sh

Code:
#!/bin/sh
# Original recepie: [URL]http://www.webupd8.org/2016/03/translate-any-text-you-select-on-your.html[/URL]
tr_id="$(wc -l /tmp/notitrans | sed -e 's/^[ \t]*//' | cut -d ' ' -f1)"
tw=$((tr_id+1))
text="$(xsel -o | perl -pe 's/-\n//g')"
translate="$(wget -U "Mozilla/5.0" -qO - "http://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=ru&dt=t&q=$(echo $text | sed "s/[\"'<>]//
g")" | sed "s/,,,0]],,.*//g" | awk -F'"' '{print $2}')"
echo "$tw:" "$text" " : " "$translate" >> /tmp/notitrans
 
# Original recepie:
I'm gonna quibble with your English spelling here: it's 'recipe', not 'recepie'. Being that anal about spelling helps avoid typos (and potentially specifying incorrect API's or ending up on a squatted domain that was intentionally mis-spelled). ;)
 
Back
Top