why sed command fail with regex - bash

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?

Related

sed - how to print regex match on particular lines

I want to print a match only of particular lines with sed;
sed '1,4 /regextomatch/p':
sed: -e expression #1, char 5: unknown command: `/'
What is a command for print?
I suggest:
sed -n '1,4{/regextomatch/p}' file
You can use quit command:
sed -n '/regex/p;4q' file
I'd rather do this:
head -n 4 filename | grep regextomatch

Sed Regex Error using \1

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?

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"

replacing string in a shell script containing specialcharacters using sed

I am facing problem with sed command to replace a text. The sed command works perfectly fine when the replacement text is a plane text without any special charachter. But my replacement text includes the unix file system path and hence it is not working. I am getting an error as below, some one please help me in this :
$ ./install2
sed: -e expression #1, char 22: unknown option to `s'
sed: -e expression #1, char 16: unknown option to `s'
$ cat install2
#!/bin/bash
CONFIG=./config
TMPFILE=./tmpFile
while read line
do
var=$(echo $line | cut -d'=' -f1)
val=$(echo $line | cut -d'=' -f2)
sed -i "s/$var/${val}/i" $TMPFILE
done < "$CONFIG"
$ cat config
INSTALLATION_ROOT=/opt/install/user-home/products
DOWNLOAD_HIBERNATE=false
CONFIG_ROOT=/opt/configs/cfgMgmt/products
$ cat tmpFile
<entry key="installationRoot">INSTALLATION_ROOT</entry>
<entry key="configDirectoryRoot">CONFIG_ROOT</entry>
Thanks,
Sunny
Try this sed -i "s|$var|${val}|i" $TMPFILE. Use | instead of / to allow path replacement.
The / characters may be uniformly replaced by any other single character within any given s command.
The / character (or whatever other character is used in its stead) can appear in the regexp or replacement only if it is preceded by a \ character.
sed -i "s#\$var#${val}#i" $TMPFILE

Resources