Substitute “[“ to [ using sed - sed

I’m trying to correct some errors of a file with the following input:
"[""test""]"
And I want to get this output:
["teste"]
I already tried the following commands:
sed -i s/"["/[/g *.csv
sed -i s/"]"/]/g *.csv
I get this error message:
sed: -e expressou-me #1, character 7: Command `s' umfinished (s/// - mísseis delimitator)
Can someone please help me?

You have to escape [ as it stands for the bracket expression opening in POSIX BRE. The closing bracket ] can be left unescaped if it appears first (see point 1 in the standard quoted above).
sed -i 's/"\["/[/g; s/"]"/]/g' file

You can try this one
sed 's/"//g;s/[a-z]\{1,\}/\"&e"/'

Related

extract substring with sed report error message

I would like to extract substring with sed as below:
#!/bin/bash
txt="[audio.sys.offload.pstimeout.secs]: [3]"
echo $txt|sed -r -e 's/\[[a-zA-Z0-9_.]+\].*/\1/'
expected output is:
audio.sys.offload.pstimeout.secs
Error message:
sed: -e expression #1, char 26: invalid reference \1 on `s' command's RHS
#!/bin/bash
txt="[audio.sys.offload.pstimeout.secs]: [3]"
echo $txt | sed -r -e 's/^\[(.*)\]:.*/\1/'
we're grabbing all the characters from the 1st [ until the last ]: and putting them in a capture group.
Would you like the regex to remain mostly like yours?
by the way - with lazy matching (which isn't supported by sed),
the regex could be cleaner, simply ^\[(.*?\])

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!

SED: unterminated `s' command at hyphen

I'm running the following in my provisioner
sed -i 's/DocumentRoot \/var\/www\/DocumentRoot \/var\/www\/app\/web-root\/\g' /etc/apache2/sites-available/000-default.conf
however I'm getting the error: sed: -e expression #1, char 69: unterminated 's' command - which is a hyphen (-) at that position. I've tried escaping it (\-) to no avail.
Any ideas?
your line:
sed -i 's/DocumentRoot \/var\/www\/DocumentRoot \/var\/www\/app\/web-root\/\g ...
^
sed needs s/.../.../g you have escaped the last / before g flag, more than that, you escaped g flag too. At least this mistake won't let your sed command go.
what better is, you pick another delimiter, if your pattern/replacement containing /(slash) too. It can save those dozens back slashes:
sed -i 's#foo/bar/blah#foo1/bar1/blah1#g` file

unescaped newline inside substitute pattern

I have a txt file with a list of 100 countries, without quotation marks around them. I am trying to change this
Canada
USA
into this
countries['Canada']=true
etc.
This is the sed command I am trying, with '\1' representing the country in quotation marks.
sed -e "s/\(.*\)/countries['\1']=true" source.txt > output.txt
The error I'm getting is
unescaped newline inside substitute pattern
What sed command do I need to achieve what I'm trying to do, and why am I getting this error
You just missed a trailing / at the end:
v
$ sed -e "s/\(.*\)/countries['\1']=true/" file
countries['Canada']=true
countries['USA']=true
Note also that you don't need to catch group, just match everything with .* and then use & to print it back:
|-------------|
vv v
$ sed -e "s/.*/countries['&']=true/" a
countries['Canada']=true
countries['USA']=true
I would just add stuff at the beginning and end:
sed -e "s/^/countries['/" -e "s/$/']=true/" source.txt > output.txt

About replacing string with sed

I'd like to replace all the \r\n with < br/ >in a document, and I'm trying this see script below
# sed -i 's/\r\n/<br/>' ~/xxd/*
however i got this error back
sed: -e expression #1, char 12: unknown option to `s'
How do i solve this problem?
Thanks!
Your problem is that you have the / separator in your replacement string so sed is assuming that's the end of your replacement, and that the > following it is a flag.
If your sed is modern enough, just use a different separator character, one that's not in the replacement string:
pax$ echo hello | sed -e 's/e/<br />/'
sed: -e expression #1, char 9: unknown option to `s'
pax$ echo hello | sed -e 's?e?<br />?'
h<br />llo
Alternatively, you can escape the offending character but I try to avoid that since it tends to lead to overly sawtooth sed commands like /\/\/\/\/\/\.
The other thing you may want to watch out for is trying to use \n in your regex since sed operates on lines anyway. If your intent is to just strip carriage returns and insert HTML line breaks, then the following sed command may be better:
s?\r$?<br />?