tags:

views:

100

answers:

3

How can you convert the following matches to the given result?

My files have the following matches

-- cut --
Lorem ipsun Hello lorem ipsun { $hello } 
@param integer $question_id                   // example of the match
Lorem ipsun Hello lorem ipsun { $hello } 
-- cut --

I would like to change them efficiently to

@param $question_id integer

My attempt in pseudo-code

perl -i.bak -pe `s/(@param) (\\w) ($\\w)/$1 $3 $2/`
+5  A: 

You mean (assuming bash shell):

perl -i.bak -pe 's/(\@param) (\w+) (\$\w+)/$1 $3 $2/'
brianary
Thank you a lot! You just saved me a day of production time :)
HH
+1  A: 
s/(\@param)\s+(\w+)\s+(\$\w+)/$1 $3 $2/g
CommuSoft
+2  A: 

I would probably either go very generic:

perl -i.bak -pe 's/^(\@param)(\s+)(\S+)(\s+)(\S+)/$1$2$5$4$3/'

or very specific:

perl -i.bak -pe 's/^(\@param)(\s+)(\$[_a-zA-Z]\w*)(\s+)(integer|long|char)(\s+)$/$1$2$5$4$3$6/'

depending on the data. The things to watch out for are the string interpolation of $identifier in the shell and the string interpolations of @param and $identifier in Perl. The first is handled by using single quotes on the shell (to prevent interpolation) and the second is handled by escaping the @ and the $ (or using a character class that avoid having to explicitly match the $).

Chas. Owens