find and replace with \| to two spaces - sed

How to replace \| to (two empty spaces) in a file
I was trying below line but it is not working
sed -i "s/\\\|/ /" c:\\test.txt

sed -i "s/\\|/ /" c:\test.txt
1 escape for \ not for | and no double \\ for file name

This might work for you (GNU sed):
sed -i "s/\\|/ /" file
The backslash needs to be escaped \\ however the pipe does not |. If you escape the pipe it becomes a metacharacter \| which is the alternative operator.

Related

how to use sed to replace all backslashes with double quote?

I am trying to use this to replace all backslashes with double quotes.
sed -e "s/'\\\\''/\"/g" nomefile
this doesn't do anything.
Input: "prova d\"amico"
Desired output: "prova d""amico"
sed -e 's/\\/"/g'
Will replace all / with "
Your issue is in the first part: \\\''/, you're looking for ' surrounded /, but you just need \\ for an escaped \
➜ cat input
"prova d\"amico"
➜
➜ sed -e 's/\\/"/g' input
"prova d""amico"
➜
There's a command specifically for global character substitutions ("transliterations"), y:
sed 'y/\\/"/'
where \ has to be escaped with another \.

How to replace \n by space using sed command?

I have to collect a select query data to a CSV file. I want to use a sed command to replace \n from the data by a space.
I'm using this:
query | sed "s/\n/ /g" > file.csv .......
But it is not working. Only \ is getting removed, while it should also remove n and add a space. Please suggest something.
You want to replace newline with space, not necessarily using sed.
Use tr:
tr '\n' ' '
\n is special to sed: it stands for the newline character. To replace a literal \n, you have to escape the backslash:
sed 's/\\n/ /g'
Notice that I've used single quotes. If you use double quotes, the backslash has a special meaning if followed by any of $, `, ", \, or newline, i.e., "\n" is still \n, but "\\n" would become \n.
Since we want sed to see \\n, we'd have to use one of these:
sed "s/\\\n/ /g" – the first \\ becomes \, and \n doesn't change, resulting in \\n
sed "s/\\\\n/ /g" – both pairs of \\ are reduced to \ and sed gets \\n as well
but single quotes are much simpler:
$ sed 's/\\n/ /g' <<< 'my\nname\nis\nrohinee'
my name is rohinee
From comments on the question, it became apparent that sed had nothing to do with removing the backslashes; the OP tried
echo my\nname\nis | sed 's/\n/ /g'
but the backslashes are removed by the shell:
$ echo my\nname\nis
mynnamenis
so even if the correct \\n were used, sed wouldn't find any matches. The correct way is
$ echo 'my\nname\nis' | sed 's/\\n/ /g'
my name is

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

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

sed - replacing text with colon

I have this command:
sed -i "s/a:b/b:c/g" file.txt
(in English: replace "a:b" with "b:c" in file.txt)
This doesn't work because of the colons in the subsitution text.
How should I re-write the command?
In case you want be safe , you can escape the : colon
sed -re "s/a\:b/b\:c/g" temp.txt
This worked for me:
-->cat 1
a:bLINE1
a:bLINE2
-->cat 1 | sed 's/a:b/b:c/g'
b:cLINE1
b:cLINE2