replacing all instances in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replacing all instances in a file
# 1  
Old 08-03-2011
replacing all instances in a file

Hi I have some script like this:

perl -i -p0e 's/"keyword1-.*?"/"keyword2"/s' file

to replace some keyword, but it replaces only the first instance of the keyword
how can i get it to replace all instances?
thanks!
# 2  
Old 08-03-2011
To replace for all instances you need to add the global option "g"

Trying doing it this way:
perl -i -p0e 's/"keyword1-.*?"/"keyword2"/g' file

---------- Post updated at 01:41 PM ---------- Previous update was at 01:40 PM ----------

To replace for all instances you need to add the global option "g"

Trying doing it this way:
perl -i -p0e 's/"keyword1-.*?"/"keyword2"/g' file

You can also refer to the blogspot
Linux,C,C++,Shell,perl
for further clarifications regarding Perl.
# 3  
Old 08-03-2011
okay thanks
what did the "s" mean?

also what does the p0e mean?
what would -pe without the 0 mean?

---------- Post updated at 01:54 PM ---------- Previous update was at 01:52 PM ----------

not sure why but it doesnt work with the "g"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all instances of a particular file in that month

Hi I need to move one file from source path to target path .Before moving , I need to go to target path and delete the files having certain naming convention in that particular month . So at any point of time while moving i should have only one file . Is there any command that can do this if... (4 Replies)
Discussion started by: Hypesslearner
4 Replies

2. Shell Programming and Scripting

List unique values and count instances in .csv file

I need to take the second column of a .csv file and count the number of instances of each unique value in that same second column. I'd like the output to be value,count sorted by most instances. Thanks for any guidance! Data example: 317476,317756,0 816063,318861,0 313123,319091,0... (4 Replies)
Discussion started by: batcho
4 Replies

3. UNIX and Linux Applications

grep file to find unique instances of username

hello - A SystemOut.log file has recurring entries that follow this format: Principal: auth9.nick.al.gov:389/USERNAME Over the course of a day thousands of lines similar to this are produced, with each username represented hundreds of times. I need to create a new file that shows... (4 Replies)
Discussion started by: 1075FJ40
4 Replies

4. Shell Programming and Scripting

Can sed replace every 2 instances it finds in a file? Pattern.

My goal is to make a script to find/replace the variable "PORT" with a unique number. Like the following <VirtualHost 174.120.36.236:PORT> ServerName architect.com.ph ServerAlias www.architect.com.ph DocumentRoot /home/architec/public_html ServerAdmin... (16 Replies)
Discussion started by: EXT3FSCK
16 Replies

5. UNIX for Dummies Questions & Answers

Deleting all instances of a certain character from a text file

In my command prompt I did: sed 's/\://' mytextfile > newtextfile But it only deleted the first instance of : in each line when some lines have multiple : appearing in each one. How can I delete all the : from the entire file? (1 Reply)
Discussion started by: guitarscn
1 Replies

6. Shell Programming and Scripting

How to select only the most frequent instances of a variable string in a file?

I've got a web access file that I want to grep (or awk or perl or whatever will work!) out the most frequent instances of unique IP entries. Meaning the file looks something like this: I'd like to run a sort or grep (or whatever) that will only select out the lines from IP's that had the... (7 Replies)
Discussion started by: kevinmccallum
7 Replies

7. Shell Programming and Scripting

Replacing string in all instances (both filenames and file contents) in a directory

Hi, I have a set of files stored in a single directory that I use to set parameters for a physics code, and I would like to streamline the process of updating them all when I change a parameter. For instance, if the files are called A2000p300ini, A2000p300sub, A2000p300run, and the text in each... (3 Replies)
Discussion started by: BlueChris
3 Replies

8. Shell Programming and Scripting

Remove all instances of duplicate records from the file

Hi experts, I am new to scripting. I have a requirement as below. File1: A|123|NAME1 A|123|NAME2 B|123|NAME3 File2: C|123|NAME4 C|123|NAME5 D|123|NAME6 1) I have 2 merge both the files. 2) need to do a sort ( key fields are first and second field) 3) remove all the instances... (3 Replies)
Discussion started by: vukkusila
3 Replies

9. Shell Programming and Scripting

Replacing all instances of an IP address in a log file.

I know this should be simple but the periods in the string replacement are throwing me off. What I want to do is replace 1.1.1.1 in my access.log file with 2.2.2.2 I have tried using 'tr' but its being thrown off by the periods and replacing all sorts of stuff in the file. What do I need... (2 Replies)
Discussion started by: LordJezo
2 Replies

10. UNIX for Dummies Questions & Answers

Multiple file instances

I am capturing text based reports with a specific program, which works no problem. However, since I send report warehouse output as they are migrated from the database software, on occasion when two capture process' initiate simultaneously, the capture file locks up. Is there a way to setup (in... (1 Reply)
Discussion started by: gozer13
1 Replies
Login or Register to Ask a Question