Sed with Regexp not working?


Sed with Regexp not working?



I'm trying to search in all files a text and replace it with the word EXAMPLE. I do the following:


for f in /home/testu/zz*; do
sed -i "s/&VAR1s*=s*'?[1]{4}'?/EXAMPLE/g" "$f"
done



It gives no error, the files seems to be "updated" in the filesystem, but they wont get changed. If I test that regexp with grep command it works fine, so it must be something about the regexp with the sed command but dunno what.



Any help?





Are you aware that [1]{4} matches 1111? Is that your intention?
– Wiktor Stribiżew
Jun 29 at 8:14



[1]{4}


1111





Ah, use sed -i "s/&VAR1s*=s*'?1{4}'?/EXAMPLE/g" or sed -i "s/&VAR1[[:blank:]]*=[[:blank:]]*'?1{4}'?/EXAMPLE/g"
– Wiktor Stribiżew
Jun 29 at 8:24



sed -i "s/&VAR1s*=s*'?1{4}'?/EXAMPLE/g"


sed -i "s/&VAR1[[:blank:]]*=[[:blank:]]*'?1{4}'?/EXAMPLE/g"





Well, it works in GNU sed. Another variation: sed -i "s/&VAR1[[:blank:]]*=[[:blank:]]*'{0,}1{4}'{0,}/EXAMPLE/g", or sed -i -E "s/&VAR1[[:blank:]]*=[[:blank:]]*'?1{4}'?/EXAMPLE/g"
– Wiktor Stribiżew
Jun 29 at 8:27



sed -i "s/&VAR1[[:blank:]]*=[[:blank:]]*'{0,}1{4}'{0,}/EXAMPLE/g"


sed -i -E "s/&VAR1[[:blank:]]*=[[:blank:]]*'?1{4}'?/EXAMPLE/g"





Did you see my examples? If you use sed -i '...' you must use 1{4,} and if you use sed -i -E '...', you may use 1{4}. You do not need [...] around 1 char, remove them.
– Wiktor Stribiżew
Jun 29 at 8:33


sed -i '...'


1{4,}


sed -i -E '...'


1{4}


[...]





So, does anything work from the ideone.com/4XGQrR? What is your sed --version? Mine says sed (GNU sed) 4.2.2
– Wiktor Stribiżew
Jun 29 at 8:55


sed --version


sed (GNU sed) 4.2.2




2 Answers
2



Your current sed command parses the regular expression as a POSIX BRE compliant pattern.


sed



In BRE POSIX, ? matches a literal ? char, and { / } also match literal { / } chars. To make a range quantifier in a BRE POSIX pattern, you need to escape {...}, {min,max}.


?


?


{


}


{


}


{...}


{min,max}



The [1] is equal to 1, so the brackets are quite redundant here.


[1]


1



To fix your pattern, you may replace ? with {0,1} (0 or 1 occurrences) and {4} with {4}:


?


{0,1}


{4}


{4}


sed -i "s/&VAR1s*=s*'{0,1}1{4}'{0,1}/EXAMPLE/g" "$f"



Thanks to Wiktor Stribiżew tips, we got the solution (SSED GNU 4.1.5). The resulting regexp works with grep and sed. The code was a mix of solutions at the end.


sed -i "s/&VAR1s*=s*'{0,}1{4}'{0,};{0,}/EXAMPLE/g" "$f"



A few things:



Things like [[:blank:]] caused error of input file.



My sed version didnt support -E, so the {} had to be escaped, didn't know that :)



Thanks again Wiktor!





To thank me, please consider accepting/upvoting my answer
– Wiktor Stribiżew
Jun 29 at 10:28






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV