Replace if regex on specific column matches expression?

I am attempting to convert rewrite rules to Nginx, and since due to the mass amount of rewrites we must convert, I've been trying to write a script to help me on a specific part, easily.

So far I have this:

rewrite ^action/static/([^\/]+)/$ staticPage.php?pg=$1&%$query_string;

What I want done is if 2nd column matches the regex that it doesn't have a "/" on it, to replace it, so the above it will convert it to:

rewrite ^/action/static/([^\/]+)/$ /staticPage.php?pg=$1&%$query_string;

If I can figure out how to change a specific column if it matches a specific regexp expression, I can do the same for the 3rd column.

So far I tried:

awk '/\^[a-f]/{gsub(/^/, "^/",2)};{print}'

Maybe I am doing this all wrong? I never fooled around with regexp, specific columns, and substituting them.

I have a very special cookie if I can figure out a solution to this :slight_smile:

Try this:

awk '/\^action\/static/{sub(/\^action/,"^/action")}{print}' file

It kinda worked.

But kinda wanted it to work with some expressions.

I had this:

awk '/\^[a-f]/{sub(/\^[a-f]/,"^/[a-f]")}{print}'

It gave me a result of this:

RewriteRule ^/[a-f]ction/videoadvertisement/$ videoAdvertisement.php?%$query_string;
RewriteRule ^/[a-f]ction/blogcategory/$ blogCategory.php?%$query_string;
RewriteRule ^/[a-f]ction/exturl/$ exturl.php?%$query_string;
RewriteRule ^/[a-f]ction/profile/avatar/$ avatarUpload.php?%$query_string;
RewriteRule ^/[a-f]ction/js/home/$ jsMyHome.php;

It kinda did the job for me, I want if it finds that the first 2-3 characters on the 2nd and 3rd column, only those columns, (there are some rules that have a 3rd, but not mess with that one) , if it does not contain like a ^/file or /file , to replace it it to look like so. Is such possible?

Been trying to learn this, as learning it I can code alot of other cool stuff with it.

Post an example of some lines and the desired output.

I have these lines:

rewrite ^action/myprofile/$ myProfile.php?%$query_string;
rewrite ^action/viewfriends/([^\/]+)/$ viewFriends.php?user=$1&%$query_string last;
rewrite ^action/profilecomments/$ profileComments.php?%$query_string;
rewrite ^action/invitation/send/$ membersInvite.php?%$query_string;
rewrite ^action/invitation/sent/$ invitationHistory.php?%$query_string;
rewrite ^action/mydashboard/$ myDashBoard.php;
rewrite ^action/videoplaylistmanage/$ videoPlayListManage.php?%$query_string last;
rewrite ^action/mydashboard/$ myDashBoard.php?%$query_string;
rewrite ^action/viewvideoplaylist/$ viewVideoPlayList.php?%$query_string;
rewrite ^action/videoplaylist/$ videoPlayList.php?%$query_string;
rewrite ^action/manageblog/$ manageBlog.php?%$query_string;
rewrite ^action/bloglist/$ blogList.php?%$query_string;
rewrite ^action/blogcomment/$ blogComment.php?%$query_string;
rewrite ^action/videoadvertisement/$ videoAdvertisement.php?%$query_string;
rewrite ^action/blogcategory/$ blogCategory.php?%$query_string;
rewrite ^action/exturl/$ exturl.php?%$query_string;
rewrite ^action/profile/avatar/$ avatarUpload.php?%$query_string;

I need them to look like:

rewrite ^/action/mydashboard/$ /myDashBoard.php;
rewrite ^/action/videoplaylistmanage/$ /videoPlayListManage.php?%$query_string last;
rewrite ^/action/mydashboard/$ /myDashBoard.php?%$query_string;
rewrite ^/action/viewvideoplaylist/$ viewVideoPlayList.php?%$query_string;
rewrite ^/action/videoplaylist/$ /videoPlayList.php?%$query_string;
rewrite ^/action/manageblog/$ /manageBlog.php?%$query_string;
rewrite ^/action/bloglist/$ /blogList.php?%$query_string;
rewrite ^/action/blogcomment/$/ /blogComment.php?%$query_string last;
rewrite ^/action/videoadvertisement/$ /videoAdvertisement.php?%$query_string;
rewrite ^/action/blogcategory/$/blogCategory.php?%$query_string;
rewrite ^/action/exturl/$ /exturl.php?%$query_string;

See on the 2nd column how it matches alot of others than "Action" as in the 2nd column, which is why I wanted to use expressions. And notice has some has 3rd columns, I just want it to search the 2nd and 3rd.

Thanks!

You have to do the changes one by one I'm afraid, I don't see common patterns to do the changes for a whole group.

Regards