There is some kind of file.xml

<data_set type="1"> <data_model type="2"> </data_model> <data_model type="3"> <data_model type="3"> </data_model> </data_model> </data_set> 

And there is a script on the regexp.sh bash

 main_file="./file.xml" while read str do if [[ "$str" =~ <data_[az]{3}+>$ ]] then echo "$str" >> $debugt else echo "$str" >> $debugf fi done < "$main_file" 

However, when executing an error

 ./regexp.sh: line 12: syntax error at line 19: `~(E)<data_[az]{3}+>$ ]] then echo "$str" >> $debugt else echo "$str" >> $debugf fi done < "$main_file"./regexp.sh: line 12: syntax error at line 19: `' unexpected 

not quite clear what the problem is. and how to fix it. Perhaps IMHO the problem is with braces

  • I do not know what exactly you want to find your regular expression, but it does not match any of the lines from the sample file. Probably, you should ask a new question, in which you should state what exactly you want to find, and what reg should look like. expression. - aleksandr barakin
  • @alexanderbarakin, well, let's say this: you need to select all the tags that match the following pattern: <data_*3 Π±ΡƒΠΊΠ²Ρ‹**ΠΏΡ€ΠΎΠ±Π΅Π»**ΠΊΠ°ΠΊΠΈΠ΅ ΡƒΠ³ΠΎΠ΄Π½ΠΎ символы ΠΊΡ€ΠΎΠΌΠ΅ >*> - I. Smirnov
  • better ask a new question. - aleksandr barakin
  • no need to pervert stackoverflow.com/questions/17333755/… bash is not needed here - strangeqargo
  • @ I.Smirnov, if you are given an exhaustive answer - check it out by clicking the check mark next to it. - ReinRaus

1 answer 1

in the line with the regular expression here:

 if [[ "$str" =~ <data_[az]{3}+>$ ]] 

special characters must be preceded by a backslash \ :

 if [[ "$str" =~ \<data_[az]{3}+\>$ ]] 

or you can put the entire regular expression in brackets:

 if [[ "$str" =~ (<data_[az]{3}+>$) ]] 
  • I tried this option, but it did not help - I. Smirnov
  • if curly brackets are removed altogether, then everything works (no errors), but the output is not exactly what you need to get - I. Smirnov
  • @I.Smirnov, I apologize, misled you: putting a regular expression in quotation marks "cancels" its effect. must be without quotes, but special characters (such as over / under characters, spaces, etc.) must be preceded by a backslash. - aleksandr barakin
  • There is no error, but on the way out is not exactly what you need to get. ps By the way, the need to escape characters is less in the if [[ "$str" =~ <data_[az]{3}+>$ ]] no expressions - I. Smirnov
  • Not special in bache, but always thought that in this case the expression should be bracketed (<data_[az]{3}+>$) - Romario