regex to change sed command+special characters? - sed

I'm using CMD on Windows Xp to replace special text with Sed. I'm using this command for replace special characters like $ or * :
sed -i "s/\*/123/g;" 1.txt
the previous command does not work because i have \, " and other special strings that sed use to make regex. The escape character ^ doesn't work well because sed no give me error but nothing change inside files.
To change this text "{\*)(//123/$$ i try use this command:
sed -i "s£"^"{^\^*)(//123/^$^$"£xx£g;" 1.txt £ is the delimiter, xx is new text..but nothing change
How i want to turn text like this? sed -i^/\\*$/$[{" ;" 1.txt into xx

This might work for you:
echo '"{\*)(//123/$$' | sed "s/[\"][{][\\][*][)][(][/][/]123[/][$][$]/xx/"
xx

Related

How to add quote at the end of line by SED

sed -i 's/$/\'/g'
sed -i "s/$/\'/g"
How to escape both $ and ' by 1 command?
This might work for you (GNU sed):
sed 's/$/'\''/' file
Adds a single quote to the end of a line.
sed 's/\$/'\''/' file
Replaces a $ by a single quote.
sed 's/\$$/'\''/' file
Replaces a $ at the end of line by a single quote.
N.B. Surrounding sed commands by double quotes is fine for some interpolation but may return unexpected results.
Use octal values
sed 's/$/\o47/'
Care to use backslash + letter o minus + octal number 1 to 3 digit
Just don't use single quotes to start the sed script?
sed "s/$/'/"
The /g at the end means to apply everywhere it's found on each stream (line) - you don't need this since $ is a special character indicating end of stream.
To add a quote at the end of a line use
sed -i "s/$/'/g" file
sed -i 's/$/'"'"'/g' file
See proof.
If there are already single quotes, and you want to make sure there is single occurrence at the end of string use
sed -i "s/'*$/'/g" file
sed -i 's/'"'*"'$/'"'"'/g' file
See this proof.
To escape $ and ' chars use
sed -i "s/[\$']/\\\\&/g" file
See proof
[\$'] - matches $ (escaped as in double quotes it can be treated as a variable interpolation char) or '
\\\\& - a backslash (need 4, that is literal 2 backslashes, it is special in the replacement), and & is the whole match.

search a string which contains "/" and replace using sed

How to search a pattern and remove the line using sed which contains special characters like "ranasnfs2:/SA_kits/prod"
I tried using a variable to hold the complete string and then recall the variable in sed command but it is not working.
echo $a
ranasnfs2:/SA_kits/prod
sed -i '/"$a"/d' test.txt
cat test.txt | grep -i SA
/SA_kits -rw,suid,soft,retry=4 ranasnfs2:/SA_kits/prod
You need to escape the slash character.
Use this for deleting lines which contain a /:
sed '/\//d' file

remove trailing spaces in a file only from non empty lines

I know how to remove all trailing spaces from a file, e.g :
sed -i 's/ *$//' file
Is there a way to do it, but not in lines containing only spaces?
Something in the spirit of :
sed -i 's/[a-zA-Z0-9;}{] *$/[a-zA-Z0-9;}{]/' file
^ keep the original characters
Preferably, but not necessariliy, with sed.
Any linux supported solution will do.
Thanks
Just make sure some other character appears before:
sed -r 's/([^\s])\s+$/\1/' file
This checks if a non-space character (\s) appears followed by any amount of spaces. If so, just print this non-space character back, so that the trailing spaces are removed.
Test
Using cat -vet to see the markers:
$ cat -vet a
hello $
$
bye $
$ sed -r 's/([^\s])\s+$/\1/' a | cat -vet -
hello$
$
bye$

SED command to replace strings with in file

I am trying to use sed to replace strings with special characters in a text file. The sed command is becoming too much complicated. If someone could please help me with the exact command.
Code -
sed -i 's;PS1='${HOSTNAME} [$ORACLE_SID] $PWD> ';PS1="${COL_YELLOW}'CUSTOMER TEST:${HOSTNAME}:[$ORACLE_SID]:$PWD> '${COL_END}";g'
I tried to escape the special characters as below but its not working.
sed -i 's;PS1=\'\${HOSTNAME} [\$ORACLE_SID] \$PWD> \';PS1="\${COL_YELLOW}\'CUSTOMER TEST:\${HOSTNAME}:[\$ORACLE_SID]:\$PWD> \'\${COL_END}";g' .bash_profile_backup
This might work for you (GNU sed):
sed -i 's|PS1='\''${HOSTNAME} \[$ORACLE_SID\] $PWD> '\''|PS1="${COL_YELLOW}'\''CUSTOMER TEST:${HOSTNAME}:[$ORACLE_SID]:$PWD> '\''${COL_END}"|g' file
N.B. ' need to be quoted in both the pattern and replacement whereas [] needs to be escaped in the pattern only.

Substitute with backslash in sed

I want to replace word with \word{sth} with sed.
I type in
sed -i s#word#\\word{sth}
but i am getting is word{sth} instead of \word{sth}
I tried with 1 slash also in the command
you should add four backslashes.
you need two to escape the backslash by the terminal, and two to escape it for sed. 2*2=4.
$ echo word|sed s#word#\\\\word{sth}#gi
\word{sth}
Consider enclosing sed expression with single-quotes '
sed -i 's#word#\\word{sth}#' file