Sed Regex Error using \1 - bash

I keep getting the following error:
sed: -e expression #1, char 29: invalid reference \1 on `s' command's RHS
grep -l 'someFiles' | xargs sed -r 's/<add fileName="\(.+?\)"/\1/g'
I am escaping the () as I have read to do in other files, I have also tried to work through it elsewhere:
https://regex101.com/r/tU9qV4/2
so I know my regex is working, I have also tested my grep, I am actually using a few exclusions and things in the statement, but it's outputting exactly what I need. Why am I still not able to use the \1?

Related

why sed command fail with regex

I am using this command
sed ':a ;{N;/\n/,/};ba'
it is showing error in regex.kindly help to identify this problem.
Full command
SAMPLE_LIST=$ (ls*fastq.gz| awk -F"_" '{print $1}' | sort | uniq | sed ':a ;{N;/\n/,/};ba')
Error bash: syntax error near unexpected token `('
Similarly another command..
FILE_List= $(ls merged_reads2/*/*join.fastq | sed ' :a ; {N;/\n/,/};ba')
error sed: -e expression #1, char 19: unterminated address regex
In the first case, bash is failing before sed can do anything because of the space between $ and (. (There's also a missing space after ls).
The sed command makes no sense: a1,a2 is the syntax to introduce an "address range". An address can be a match, so /\n/ is one (which never matches, because the newlines are not part of the line contents sed matches against), but / is not a valid address. What are you trying to match?

bash command sed is outputting an unexpected error

I'm trying replace a word in a file using sed.
In the same bash script I use the command :
sed -i "s/${list[$index]}/${phone}/g" $1
And it's working flawlessly on the first function, but the second function I wrote:
sed -i "s/${list[$index]}/${zipcode}/g" $1
Outputs this error:
sed: -e expression #1, char 0: no previous regular expression
I'm really desperate, I'm pretty sure that it's a dumb mistake I'm doing but I can't sort it out
When the first half of a sed substitute command is empty:
sed 's//foo/' <<< bar
It returns this error:
sed: -e expression #1, char 0: no previous regular expression
Therefore, as William Pursell commented, there's a value of the ${list[#]} array that's empty, or maybe $index is out of the array's range.

Unix Remove all occurences of character and save

How to remove all occurences of string "???" of a file and save it?
My approach so far:
cat file.txt | sed -ie '/s/???//' file.txt
However I get the following error:
sed: -e expression #1, char 4: unknown command: `?'
You can use this sed command:
sed -i 's/???//g' file.txt
There is no reason to use cat here as sed can directly operate on a file and save it in-line.
Also note that unlike other regex flavors BRE (Basic Regular Expressions) which is default regex engine of sed doesn't treat ? as a special regex meta character hence there is no need to escape ? here.

sed behavior on cygwin inconsistent

i have been facing an issue with a certain sed expression working on one windows system and breaking on other. The shell script is being run on cygwin, where the cygwin and sed versions are identical.
expression:
url=$(echo ${!1} | sed 's/{/\\\{/g'); \
error on the system is :
Error: /usr/bin/sed: -e expression #1, char 2: unterminated `s' command
So when i use it by escaping the braces it works:
url=$(echo ${!1} | sed 's/\{/\\\{/g'); \
But the breaks on the other with error saying:
sed: -e expression #1, char 11: Invalid preceding regular expression
Please help .. stuck with this for some time. Please let me know if you need more details.
for a substitution by \{
echo ${!1} | sed -e 's/{/\\{/g'
or by \\{
echo ${!1} | sed -e 's/{/\\\\{/g')
3 \ and no explicit -e are certainly the problem.
Info: No error with your code on my cygwin (usinf a direct string in place of ${!1}
If you escape the first {, sed is waiting for a number of occurence regex and an closing brace (ex: \{1,\})

Find and Replace string using sed gives error

I am using shell script. My requirement is to find and replace the string. The string contains "/" char as well. I am getting error sed: -e expression #1, char 18: unterminated `s' command. Can someone tell how should i replace the string which has "/"?
#!/bin/bash
...
search_string="../conf/TestSystem/Inst1.xml"
rep="Inst1/Instrument.xml"
sed -i 's|${line}|${rep}/g' MasterConfiguration.xml
I tried using another sed command but that one also gave error sed: -e expression #1, char 13: unknown option to `s'
sed -e "s/${line}/${rep}/g" MasterConfiguration.xml > tempfile
Whenever you deal with shell-variables you have to get them out of the "sed-string":
For example:
sed -e "s/"${line}"/"${rep}"/g" MasterConfiguration.xml > tempfile
Otherwise sed will treat the chars as-is and search for ${line} literally:
As you see, nothing happens here.
Furthermore, if your variables contain / you need to use another delimiter for sed. I tend to use ~ in such a case, but you're free to use other chars - just be consequent and don't mix them like in your first example-sed-command:
sed 's~'${line}'~'${rep}'/g' //WRONG
sed 's~'${line}'~'${rep}'~g' //RIGHT
Combine both and it will work:
You can try this sed,
sed -i "s#${line}#${rep}#g" MasterConfiguration.xml
Problem:
Instead you have,
sed -i "s|${line}|${rep}/g" MasterConfiguration.xml
It should be,
sed -i "s|${line}|${rep}|g" MasterConfiguration.xml
Syntax:
sed "s|pattern|replacement|g"

Resources