0

I need help with sed, actually with Linux regular expressions:
I need to change two parameters between parentheses, for instance, if the line is

 mysqli_select_db($par1, $par2)<br/>

I need to change it to

 mysqli_select_db($par2, $par1)<br/>

I need something generic, they aren't always going to be par1 and par2.

3 Answers 3

4

A more pedantic approach with sed:

sed 's/\(mysqli_select_db(\)\([^,]*\)\(,[[:space:]]*\)\([^)]*\))/\1\4\3\2)/g' file
  • s/ substitute
  • \( start capturing group 1
  • mysqli_select_db( match literal string
  • \) end capturing group 1
  • \( start capturing group 2
  • [^,]* match any non-comma characters
  • \) end capturing group 2
  • \( start capturing group 3
  • ,[[:space:]]* match comma and optional space characters
  • \) end capturing group 3
  • \( start capturing group 4
  • [^)]* match any non-) characters
  • \) end capturing group 4
  • ) closing ) of command
  • /\1\4\3\2)/g replace with capturing groups 1, 4, 3, 2 and closing )
1
  • Ok, you're a genius. That was it, what I needed. Thanks man. Jul 8, 2019 at 18:32
1

You can use:

sed -E 's/(.*\()(.*), (.*)(\).*)/\1\3, \2\4/g' file_name

e.g.

echo 'mysqli_select_db($par1, $par2)<br/>' | sed -E 's/(.*\()(.*), (.*)(\).*)/\1\3, \2\4/g'

will give output as:

mysqli_select_db($par2, $par1)<br/>
3
  • Yes, in this case, ok. But I want something generic. Those words between parenthesis can be anything and I have to put on a script to change dozens os files. Jul 8, 2019 at 18:26
  • @CarlosParisotto it will always work as long as you don't remove braces. This command doesn't depends on <br/>. The only condition is that there should be two values inside the braces.
    – Prvt_Yadav
    Jul 8, 2019 at 18:28
  • (1) Trivially, this assumes that there is only one pair of parentheses.  More precisely, if there are multiple pairs of parentheses, this operates on the last one.  (2) Unlike the accepted answer, this allows any function name before the (.  (The question does not say whether mysqli_select_db should be hardcoded.)  (3) Perhaps more importantly, this assumes that there is exactly one space after the comma.  (The question does not say whether this should be variable.) … … … … … … … … … … … … That said, this seems to be as good as the accepted answer. Jul 11, 2019 at 21:41
1

This is a crude example based on your example above. You can of course adapt.

The key is to identify the two groups you want to invert and use them in the second part.

$ echo 'mysqli_select_db($par1, $par2)<br/>' | sed 's/mysqli_select_db(\(.*\), \(.*\))/mysqli_select_db(\2, \1)/'

results in

mysqli_select_db($par2, $par1)<br/>
1
  • Except that I don't have a common identification. Those words between parenthesis can be anything and I have to put on a script to change dozens os files. Jul 8, 2019 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.