Replace if regex on specific column matches expression?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace if regex on specific column matches expression?
# 1  
Old 11-25-2008
Error 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 Smilie
# 2  
Old 11-25-2008
Try this:

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

# 3  
Old 11-25-2008
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.
# 4  
Old 11-25-2008
Post an example of some lines and the desired output.
# 5  
Old 11-25-2008
I have these lines:

Code:
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:

Code:
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!

Last edited by EXT3FSCK; 11-25-2008 at 02:04 PM..
# 6  
Old 11-25-2008
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Regex Expression Replace

I have a XML file where there is a tag with like <wd:address_line_1>1234 Street</wd:address_line_1> I want to replace the values "1234 Street" with "Test Data". Different people have different address lines and i want to replace with a fixed value to mask the file. I was trying to use sed... (7 Replies)
Discussion started by: dr46014
7 Replies

2. Shell Programming and Scripting

Search Replace Specific Column using RegEx

Have Pipe Delimited File: > BRYAN BAKER|4/4/2015|518 VIRGINIA AVE|TEST > JOE BAXTER|3/30/2015|2233 MockingBird RD|ROW2On 3rd column where the address is located, I want to add a space after every numeric value - basically doing a "s//&\ / ": > BRYAN BAKER|4/4/2015|5 1 8 VIRGINIA AVE|TEST > JOE... (5 Replies)
Discussion started by: svn
5 Replies

3. Shell Programming and Scripting

CSV file REPLACE COLUMN if it matches

I have a file cat 1.txt AAAA , BBBB , CCCC , DDDD DFDF , DFDF , DFDF , FDDD AA11 , DFDF , 0000 , UTIO ADSD , WERT, 0000 , JKJL If the 3rd column is not equal to "0000" , then it should replace "0000" with "XXXX" and if its equal to "0000" then print the line as it is. need help. (9 Replies)
Discussion started by: aravindj80
9 Replies

4. Shell Programming and Scripting

Replace a specific column with a specific value

Hi, I am looking to replacing value of a specific column of /etc/pam.d/system-auth file. My file looks like this password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok expected result password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok... (5 Replies)
Discussion started by: Litu1988
5 Replies

5. Shell Programming and Scripting

Help with replace specific column command

Input file: ASD_QAW 12 A_@ AE_AQ 21 PA_123 ASDA_@ 23 ADA_AS . . Output file: ASD_QAW 12 A @ AE_AQ 21 PA 123 ASDA_@ 23 ADA AS . . Do anybody know how to just specific and replace "_" in column 3 with tab delimiter (\t)? Thanks for advice. (2 Replies)
Discussion started by: perl_beginner
2 Replies

6. Shell Programming and Scripting

Bash Replace value in specific column

Hi all, I have two files with the following format: file1 BBB;33 AAA;2 CCC;5 file2 5;.;.;. 33;.;.;. The first file contain a list of code and numbers. The second file only the number. I would like to replace the corresponding code in the first column of the file1 with the... (3 Replies)
Discussion started by: g256
3 Replies

7. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies

8. Shell Programming and Scripting

SED Replacing all but one regex match on a line or specific matches

Hi, I'm attempting to rename some files that have spaces in them. Without linking sed commands together is it possible to replace the first three "." to " ". File.name.is.long.ext -> File name is long.ext I can get the desired effect with echo "File.name.is.long.ext" | sed 's/\./ /g;s/... (5 Replies)
Discussion started by: vectox
5 Replies

9. Shell Programming and Scripting

replace a column in a file if it matches certain pattern

Hi, I want to replace a column in a file if it matches certain pattern. Can you help me on this. Here is the file content. 000000 1111111 2222222 011111 0123445 1234556 023445 1111111 2343455 if second column contains 1111111 i need to replace it with 0000000 Can you... (6 Replies)
Discussion started by: Krrishv
6 Replies

10. Shell Programming and Scripting

How to replace a specific word in specific column?

Hi My orginal file is like (100s of lines) id host ip location remarks 1 host1 ip1 - xxx 2 host2 ip2 - xxx 3 host3 ip3 - xxx -- -- 9 host9 ip9 - xxx I have a ref file like host1 location1 host2 location2 host3 location3 -- --... (6 Replies)
Discussion started by: ./hari.sh
6 Replies
Login or Register to Ask a Question