Replace & with chr(38) using SED - sed

I am trying to replace & using chr(38)using sed but I keep getting error.
cat line.txt -- Partners & Customers
sed 's/&/' || chr(38) || '/g' line.txt
I get below error.
-bash: syntax error near unexpected token `38'
I tried escaping ( and ) but it didn't help.
Regards.

Related

I'm getting "Invalid range end" for sed command in RHEL 8.3

I was using this junk character filtering command in RHEL 6.10 which was working perfectly.
sed 's/[^][A-Za-z0-9\^`~!##$%&*|\,:;{}()+=_-./ "<>?\/\\]//g'
However, in RHEL 8.3 below error happens.
sed: -e expression #1, char 54: Invalid range end
Any advice is much appreciated
Use this script:
> cat test.txt
$jeden$
#pięć#
!łzy!
> sed 's/[^][A-Za-z0-9^`~!##$%&*|,:;{}()+=_. "<>?\/\-]//g' test.txt > test2.txt
> cat test2.txt
$jeden$
#pi#
!zy!
All characters different from those mentioned in negated brackets are deleted from test file and saved in test2 file safely.

sed - 'extra character after the command'

having trouble with a sed command.
I'm looking to find a line in a file and replace it.
In my script I've used this command without issue; (I use it to set variables)
sed -i '/job=empty/c\job='$job'' $sd/pingcheck-mon-$job.sh
The line I want to replace looks like this,
bash home/user/pingcheck/pingcheck-jobs/job1/pingcheck-mon-job1.sh
This is the command I can't get to run:
sed -i '/bash '$sd'/pingcheck-mon-'$job'.sh/c\jobslot=empty' $wd/pingcheck-worker.sh
Error I get:
sed: -e expression #1, char 9: extra characters after command
Could someone please tell me where I'm going wrong?
Thanks in advance!

Error using sed command to replace references to include file that has changed name

I've renamed an include file and I'd like to replace all references to it in my code, all files are in the same directory.
After researching several websites, I keep getting errors when I try the following (however it seems to work for others, maybe because I'm using Mac OS X)
grep -rl 'old-file.php' . | xargs sed -i 's/old-file.php/new-file.php/g'
I seem to get an error relating to the first grep matched file:
sed: 1: "./somefile.php": invalid command code .
I've tried various forms of escaping the . ([.] and .) neither seem to work.
Any helpful suggestions are most welcome!
grep -rl 'old-file.php' . | xargs sed -i 's#old-file.php#new-file.php#g'
use other separator than / in this sed (or escape the /)
now if the ./someFile is in the nsearch pattern (old-file) use maybe [.] or \. in this part of the sed. to avoid other matching value like (ba)d/old_file.php

Sed command returning "invalid command code"

I'm trying to insert this text...
"error.emailNotActivated":"This email address has not been activated yet."
... at line number 5 using sed.
Here is my command so far
translated="This email address has not been activated yet.";
sed -i '' -e '5i\'$'\n''"error.emailNotActivated":'"\"$translated\"" local.strings;
I unfortunately keep getting the error message "invalid command code T".
It seems that sed is interpreting the colon as part of a command.
Any suggestions how i can avoid this?
EDIT:
Seems like an update error (working with old file d'oh...)
the above expression works fine as do the other suggestions.
Why are you fighting with sed for this? It's trivial in awk:
awk -v line='"error.emailNotActivated":"'"$translated"'"' '
NR==5{print line} {print}
' file
or:
awk -v line="\"error.emailNotActivated\":\"${translated}\"" '
NR==5{print line} {print}
' file
Are you looking for something like this?
$ seq 1 5 > file
$ cat file
1
2
3
4
5
$ translated="\"error.emailNotActivated\":\"This email address has not been activated yet.\""
$ echo $translated
"error.emailNotActivated":"This email address has not been activated yet."
$ sed -i "5i $translated" file
$ cat file
1
2
3
4
"error.emailNotActivated":"This email address has not been activated yet."
5

unwrap text file - Can't find string terminator ERROR

I got a LDAP schema but ldifde wraps long lines so after googled I found this command to fix the file, but I'm getting the following error:
c:\Perl64\bin>perl -p -e 'BEGIN {$/ = undef} s/\n(?=[a-z])/ /g' test.ldf
Can't find string terminator "'" anywhere before EOF at -e line 1.
c:\Perl64\bin>
Even replacing the content with a single line I get the same error so I assume the sentence is wrong.
Could you give some clue about that, Im newbie on perl.
Thanks,
m0dest0.
You seem to be on windows. Windows does not recognize single quote ', you need to use double quote ":
c:\Perl64\bin>perl -pe "BEGIN {$/ = undef} s/\n(?=[a-z])/ /g" test.ldf
You should be aware that this does not change the input file, it just prints to standard output. If you want to alter the file, you can either add the in-place edit switch, e.g. -i.bak (saves backup in test.ldf.bak) or use redirection:
c:\Perl64\bin>perl -pe "BEGIN {$/ = undef} s/\n(?=[a-z])/ /g" test.ldf > out.ldf
Wild guess, but could it be that quoting is messing you up, in the command prompt? Try putting that one line in a file, and see if perl -p file.pl test.ldf would run.