-2

I am stuck with situation, I have string as shown below:

-name "B_12*" -o -name "B_21*" -o -name "B_31" -o -name "B_41"

My requirement is I want to convert above string is as shown below:

-name "B_12*.tar" -o -name "B_21*.tar" -o -name "B_31.tar" -o -name "B_41.tar"

I am not expert with bash commands but I have little bit idea the problem could be solved with sed command.

oguz ismail
  • 1
  • 16
  • 47
  • 69
Qasim
  • 9,058
  • 8
  • 36
  • 50
  • You should add more details on what exactly needs to be replaced. Is it always `-name "B_XX*"` or something else? – James Z Mar 15 '20 at 09:26
  • Show us what you have tried and the problem it has. – revo Mar 15 '20 at 10:56
  • 1
    It looks like you're trying to construct a list of arguments (for the `find` command?) as a string. This is almost always the wrong way to do it. The usual way to do it right is with an array instead of a plain string (see [here](https://stackoverflow.com/questions/12136948/why-does-shell-ignore-quotes-in-arguments-passed-to-it-through-variables)). – Gordon Davisson Mar 15 '20 at 16:27

2 Answers2

3

The only tricky part here is that you need to match both quotes so that they won't be matched again. With a sed distro which has ERE support by -E option, following command would suffice.

sed -E 's/("[^"]*)"/\1.tar"/g' file
oguz ismail
  • 1
  • 16
  • 47
  • 69
1

This pattern will match the text string without single quote , all you need to do is get all the matches and perform an alternate query to add .tar

\b[A-Z][^"]+

enter image description here

Dang D. Khanh
  • 1,440
  • 6
  • 13
  • I order to have the same results as regex101.com, I think you need to add the `-E` flag to the *sed* command. – Pierre François Mar 15 '20 at 11:16
  • Even with -E some seds will not recognize `\b` – oguz ismail Mar 15 '20 at 12:00
  • Hi Thanks for your answer.. What would be regex if i want to search only files having number before the extension example: B_12.csv, B_22.csv and B_22-UK.csv I want regex whic should get me B_12.cvs and B_22.csv and should exlcude B_22-UK.csv – Qasim Mar 18 '20 at 09:06