Need regex shell script to remove text from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need regex shell script to remove text from file
# 1  
Old 09-29-2012
Need regex shell script to remove text from file

Hello
I am trying to remove a line like <?php /*versio:2.05*/if (!defined('determinator')){ content goes here}?>
Now i want to scan all php file and remove that above line. I already write it but not working.
Code:
#!/bin/bash
find . -name '*.php' -exec grep -q determinator {} \; -print -exec perl -pi -w -e 's/\<\?php \/*versio:2.05;*/ if \(\!defined\(\$determinator.*\?\>//g' {} \;

Moderator's Comments:
Mod Comment Please use code tags

Can somebody help to solve that.

Thanks

Last edited by jim mcnamara; 09-29-2012 at 08:44 PM..
# 2  
Old 09-29-2012
Code:
find . -name "*.php" -exec grep -q determinator {} \; -print -exec perl -pe 's#<\?php /\*versio:2\.05\*/if \(\!defined\(\$determinator\)\)\{.*?\}\?>##g' {} \;

# 3  
Old 09-30-2012
Or perhaps more readably:
Code:
find . -name '*.php' | while read file ; do
   perl -pi -e 's...' $file
done


Last edited by mirni; 09-30-2012 at 03:53 AM.. Reason: fixed -pie
# 4  
Old 09-30-2012
Shouldn't perl -pie be perl -pi -e?
This User Gave Thanks to elixir_sinari For This Post:
# 5  
Old 09-30-2012
Yes, thanks. As you can probably tell, my perl skills are not very developed... :-\
# 6  
Old 09-30-2012
Hi balajesuri

Thank you for your reply.

I run the code you provided
Code:
find . -name "*.php" -exec grep -q determinator {} \; -print -exec perl  -pe 's#<\?php /\*versio:2\.05\*/if \(\!defined\(\$determinator\)\)\{.*?\}\?>##g' {} \;

But it just print me the entire file code

I want to update the file and remove the code i.e /*versio:2.05*/if (!defined('determinator')){ full content }

Last edited by Franklin52; 09-30-2012 at 05:57 PM.. Reason: Please use code tags
# 7  
Old 09-30-2012
@devp: Please include the option -i to perl code.

...-exec perl -i -pe 's....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

2. Programming

awk processing / Shell Script Processing to remove columns text file

Hello, I extracted a list of files in a directory with the command ls . However this is not my computer, so the ls functionality has been revamped so that it gives the filesizes in front like this : This is the output of ls command : I stored the output in a file filelist 1.1M... (5 Replies)
Discussion started by: ajayram
5 Replies

3. Shell Programming and Scripting

Shell script to remove oldest file

Hi, I have two set of files in a directory called /tmp/backup. I want to write a script to remove the oldest of these two, so that there is space available for a newer similar backup file. For example, I have /tmp/backup/dbbackup_dbname_121223112 (oldest)... (4 Replies)
Discussion started by: welldone
4 Replies

4. Shell Programming and Scripting

How to remove a file in shell script if its size exceeds limit?

How can i remove a file using shell script when its size exceeds 10MB. Given that file is located in different location to the shell script where it is running? (4 Replies)
Discussion started by: vel4ever
4 Replies

5. Shell Programming and Scripting

Shell script to remove some content in a file

How can I remove all data that contain domain e.g zzgh@something.com, sdd@something.com.my and gg@something.my in one file? so that i only have data without the domain in the file. Here is the file structure "test.out" more test.out 1 zzztop@b.com 1 zzzulll 1 zzzullll@s.com.my ... (4 Replies)
Discussion started by: Mr_47
4 Replies

6. Shell Programming and Scripting

How to remove particular line from file through shell script?

Hi, I am pasring a file line by line. I need to check each field in line and remove particular line. input file lines are, 02;ABC;PQR 03;aaa;rrr 04;ABC;ggg 09;eee;ABC 04;lmn;stu I am looking for line containing "ABC" as field value. Now How can I remove this line from input file... (7 Replies)
Discussion started by: Poonamol
7 Replies

7. Shell Programming and Scripting

Shell Script to remove spaces while writing to the file

Hello Folks, I want to get the results from a SQL query which needs to be exported to a .txt file. My Script is something like #!/bin/ksh db2 connect to DATABASE user user_name using pwd; touch test.txt isResult=0; isResult= `db2 -x select 'ABC',COL_B from TABLE_A WHERE COL_B=CONDITION`... (6 Replies)
Discussion started by: dinesh1985
6 Replies

8. Shell Programming and Scripting

Shell script regex help: accept only 3 file extensions

This regex is supposed to accept files with extensions 270, 276, and "txt" only. Everything else should be discarded. This is what I have. I'll spare you the rest of the code. ext =".\$" #ext =".\$" #ext =".\$" #ext =".\$" for xfile in `ls $dir | grep "$ext" | xargs`; do... (9 Replies)
Discussion started by: grep01
9 Replies

9. Shell Programming and Scripting

regex to remove text before&&after comma chars

Hi, all: I have a question about "cleaning up" a huge file with regular expression(s) and sed: The init file goes like this: block1,blah-blah-blah-blah,numseries1,numseries2,numseries3,numseries4 block2,blah-blah-blah-blah-blah,numseries,numseries2,numseries3,numseries4 ...... (3 Replies)
Discussion started by: yomaya
3 Replies

10. Shell Programming and Scripting

Shell script to remove duplicates lines in a file

Hi, I am writing a shell script that needs to remove duplicate lines within a file by category. example: section a a c b a section b a b a c I need to remove the duplicates within th category with out removing the duplicates from the 2 different sections (one of the a's in section... (1 Reply)
Discussion started by: RichElks
1 Replies
Login or Register to Ask a Question