sed finding | and replacing with " quotation marks - replace

I need to replace each | with " everywhere in a text file.
I have tried this below and many other variations and I cannot get it to work.
sed -i 's/"|"/"\""/g' workingfolder/start.txt > workingfolder/sortish.txt
sed -i 's/"|"/"\""/g' workingfolder/start.txt > workingfolder/sortish.txt
I really hope someone can help me.

this??
echo "||||"|sed 's/|/"/g'
""""
If this was what you were looking for,
you don't have to quote chars in s/../.. expression
you can wrap quote (single/double) with the other (double/singel).

Related

Extracting Substring from String with Multiple Special Characters Using Sed

I have a text file with a line that reads:
<div id="page_footer"><div><? print('Any phrase's characters can go here!'); ?></div></div>
And I'm wanting to use sed or awk to extract the substring above between the single quotes so it just prints ...
Any phrase's characters can go here!
I want the phrase to be delimited as I have above, starting after the single quote and ending at the single-quote immediately followed by a parenthesis and then semicolon. The following sed command with a capture group doesn't seem to be working for me. Suggestions?
sed '/^<div id="page_footer"><div><? print(\'\(.\+\)\');/ s//\1/p' /home/foobar/testfile.txt
Incorrect would be using cut like
grep "page_footer" /home/foobar/testfile.txt | cut -d "'" -f2
It will go wrong with single quotes inside the string. Counting the number of single quotes first will change this from a simple to an over-complicated solution.
A solution with sed is better: remove everything until the first single quote and everything after the last one. A single quote in the string becomes messy when you first close the sed parameter with a single quote, escape the single quote and open a sed string again:
grep page_footer /home/foobar/testfile.txt | sed -e 's/[^'\'']*//' -e 's/[^'\'']*$//'
And this is not the full solution, you want to remove the first/last quotes as well:
grep page_footer /home/foobar/testfile.txt | sed -e 's/[^'\'']*'\''//' -e 's/'\''[^'\'']*$//'
Writing the sed parameters in double-quoted strings and using the . wildcard for matching the single quote will make the line shorter:
grep page_footer /home/foobar/testfile.txt | sed -e "s/^[^\']*.//" -e "s/.[^\']*$//"
Using advanced grep (such as in Linux), this might be what you are looking for
grep -Po "(?<=').*?(?='\);)"

Cywgin Sed match not working when "/" in string

testLine="This is a test line: Asia/Pacific Australia"
expr="Asia\/Pacific Australia"
This works:
echo "$testLine" | sed 's/Asia\/Pacific Australia/TEST/g'
This DOES NOT:
echo "$testLine" | sed 's/$expr/TEST/g'
I've tried everything from using multiple "escapes", using different quote marks, using -r and -re Sed switches. Nothing seems to work.
Please advise if anyone has a working solution. Please can someone advise and provide the Cygwin output here, many thanks!
First change your variable as
expr="Asia/Pacific Australia"
this should work then
echo "$testLine" | sed 's_'"$expr"'_TEST_g'
note that for sed delimiters you can choose other chars as well, here _

SHELL - SED replacing text between " "

I'm new at shell and SED in general. I am trying to replace a text in a file. This text is between " " and i am struggling to create the command...
The file has something like:
download_amule_tcp_port="42450"
My code:
TCPPort=44444
sed -i 's/^download_amule_tcp_port=\".*/download_amule_tcp_port=\"$TCPPort\"/' settings.conf
It is not working...
download_amule_tcp_port=$TCPPort"
Thanks for any help!
M
==== EDIT ==== The correct syntax as in comments, should be:
TCPPort="44444"
sed -i "s/^download_amule_tcp_port=.*/download_amule_tcp_port=\"$TCPPo
rt\"/" settings.conf
This will work. thanks!
the problem lies in the single quote, which prevents the variable,$TCPPort in this case, from being expanded to "44444".
the following code should work
TCPPort="44444"
sed -i 's/^download_amule_tcp_port=".*/download_amule_tcp_port="'"$TCPPort"'"/g' settings.conf
Use double quotes instead of single quotes and escape the inner double quotes:
sed -i "s/^download_amule_tcp_port=\".*/download_amule_tcp_port=\"$TCPPORT\"/" settings.conf
Btw, you set the variable TCPPort instead of TCPPORT.

how to replace spaces inside doublequotes

I need to replace every spaces inside doublequotes in a variable:
VAR='"this is my problem" but not yours'
Now i have to replace the spaces (may be more than one in a row) in "this is my problem" with '[[:space]]+'. My shell is busybox. What is the simplest way?
Thank you.
Try this: echo $var | sed -re 's|.*("[^"]*").*|\1|g' | sed -re 's|\s|[[:space:]]+|g'. First sed is to extra the double quotes part, the second sed is to turn the spaces inside the double quotes into [[:space:]]+.
PS. You could have saved two hours if you put all the stuff in the comments into your original question from the start :)

Replace text using sed

i am having trouble replacing the modified date in my script via sed.
I am getting the last modified date like this:
olddate=`grep -m1 "Built " script.sh | cut -c 22-29`
I get the current date with:
newdate=`date +%d/%m/%y`
Basically i want to replace old date with new date
sed -i "" "s/$olddate/$newdate/g" script.sh
But this doesn't work as the date contains slashes. I've looked around and i can't find the way to escape them properly. Any help would be appreciated.
You can use separators other than slashes, for instance ";"
sed -i "" "s;$olddate;$newdate;g" script.sh
Use , instead of / !
sed -i "" "s,$olddate,$newdate,g" script.sh
In fact you can use almost any char as separators.
use sed "s#$olddate#$newdate#g"
that should work