How to search/replace a directory path in a file using perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to search/replace a directory path in a file using perl
# 1  
Old 12-20-2011
How to search/replace a directory path in a file using perl

Hello All,

Here is what I am trying to do and maybe you guys can point me in the right direction. I have a file that contains the following string(s):

WARNING: </d1/test/program1> did not find item1
WARNING: </d1/test/program1> item1 does not exist
WARNING: </d1/test/program2> item1 failed to load

WARNING: </d1/test/program1> did not find item2
WARNING: </d1/test/program1> item2 does not exist
WARNING: </d1/test/program2> item2 failed to load


What I am trying to do is search for the string "WARNING: </d1/test/program1> did not find" and replace it with "ERROR: </d1/test/program1> did not find"

Here is what I have so far:

Code:
prg_env=test
prg_dir=/d1/$prg_env

perl -pi -e "s/WARNING: \<${prg_dir}\/program1\> did not find/ERROR: \<${prg_dir}\/program1\> did not find/g" filename

Obviously, the above fails because I have / in the directory path. When I change prg_dir as follows to include the escape character \, it works:

Code:
prg_dir="\/d1\/$prg_env"

Question: Is there a way around not having to change the $prg_dir as above, and still perform the search and replace using perl? The values for $prg_env and $prg_dir are actually set in another script depending on the environment (i.e. test or prod).

Thanks!
# 2  
Old 12-20-2011
Quote:
Originally Posted by maxshop
...I have a file that contains the following string(s):

WARNING: </d1/test/program1> did not find item1
WARNING: </d1/test/program1> item1 does not exist
WARNING: </d1/test/program2> item1 failed to load

WARNING: </d1/test/program1> did not find item2
WARNING: </d1/test/program1> item2 does not exist
WARNING: </d1/test/program2> item2 failed to load


What I am trying to do is search for the string "WARNING: </d1/test/program1> did not find" and replace it with "ERROR: </d1/test/program1> did not find"

Here is what I have so far:

Code:
prg_env=test
prg_dir=/d1/$prg_env
 
perl -pi -e "s/WARNING: \<${prg_dir}\/program1\> did not find/ERROR: \<${prg_dir}\/program1\> did not find/g" filename

Obviously, the above fails because I have / in the directory path. When I change prg_dir as follows to include the escape character \, it works:

Code:
prg_dir="\/d1\/$prg_env"

Question: Is there a way around not having to change the $prg_dir as above, and still perform the search and replace using perl? The values for $prg_env and $prg_dir are actually set in another script depending on the environment (i.e. test or prod).

...
Code:
$
$ cat f45
WARNING: </d1/test/program1> did not find item1
WARNING: </d1/test/program1> item1 does not exist
WARNING: </d1/test/program2> item1 failed to load
WARNING: </d1/test/program1> did not find item2
WARNING: </d1/test/program1> item2 does not exist
WARNING: </d1/test/program2> item2 failed to load
$
$
$ prg_env=test
$ prg_dir=/d1/$prg_env
$
$ echo $prg_env
test
$
$ echo $prg_dir
/d1/test
$
$ export prg_env
$ export prg_dir
$
$
$ perl -pi.bak -e "s|WARNING: <${prg_dir}/program1> did not find|ERROR: <${prg_dir}/program1> did not find|g" f45
$
$
$ # Modified file
$ cat f45
ERROR: </d1/test/program1> did not find item1
WARNING: </d1/test/program1> item1 does not exist
WARNING: </d1/test/program2> item1 failed to load
ERROR: </d1/test/program1> did not find item2
WARNING: </d1/test/program1> item2 does not exist
WARNING: </d1/test/program2> item2 failed to load
$
$
$ # Backup file
$ cat f45.bak
WARNING: </d1/test/program1> did not find item1
WARNING: </d1/test/program1> item1 does not exist
WARNING: </d1/test/program2> item1 failed to load
WARNING: </d1/test/program1> did not find item2
WARNING: </d1/test/program1> item2 does not exist
WARNING: </d1/test/program2> item2 failed to load
$
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Convert Relative path to Absolute path, without changing directory to the file location.

Hello, I am creating a file with all the source folders included in my git branch, when i grep for the used source, i found source included as relative path instead of absolute path, how can convert relative path to absolute path without changing directory to that folder and using readlink -f ? ... (4 Replies)
Discussion started by: Sekhar419
4 Replies

2. Shell Programming and Scripting

Sizeof a file from directory path in perl

Hai how to find size of a file?? ex : /home/kiran/pdk/sample/calibre this is a path In that I have to find size of a files in side a calibre(it is the folder) like .results or .summary (1 Reply)
Discussion started by: kiran425
1 Replies

3. Shell Programming and Scripting

perl- read search and replace string from the file

Dear all, I have a number of files and each file has two sections separated by a blank line. At the top section, I have lines which describes the values of the alphabetical characters, # s #; 0.123 # p #; 12.3 # d #; -2.33 # f #; 5.68 <blank line> sssssss spfdffff sdfffffff Now I... (4 Replies)
Discussion started by: sasharma
4 Replies

4. Shell Programming and Scripting

New to Perl Mail @ sign search and replace in a file

HI I'm terribly new to perl .. I;ve been trying to use this command to search and replace entries in a file I tried this and it works perl -p -i -e 's/old/new/' filename Problem is that I have a list of email addresses and I need to serach and replace the entire email address as my... (5 Replies)
Discussion started by: mnassiri
5 Replies

5. Shell Programming and Scripting

How to search a date format from a file an replace with a string in PERL

I am very new to Perl. I am struggling so hard to search a date (such as 10/09/2009, 10-09-2009) from a text file and replace with a string (say DATE) using Perl. Please help me out. Thanks in advance. Regds Doren (4 Replies)
Discussion started by: my_Perl
4 Replies

6. Shell Programming and Scripting

Perl search and replace file content.

I am not sure if this is doable. I am trying to open and print the content of the file by replacing all instances fo perl to PERL . This is my code but it is giving me the number count instead of the actual lines with changes. open (PERLHISTORY, 'sample.txt') or die "The file sample.txt could... (3 Replies)
Discussion started by: jxh461
3 Replies

7. UNIX for Dummies Questions & Answers

how to use sed or perl command to find and replace a directory in a file

how to use sed command to find and replace a directory i have a file.. which contains lot of paths ... for eg.. file contains.. /usr/kk/rr/12345/1 /usr/kk/rr/12345/2 /usr/kk/rr/12345/3 /usr/kk/rr/12345/4 /usr/kk/rr/12345/5 /usr/kk/rr/12345/6 /usr/kk/rr/12345/7... (1 Reply)
Discussion started by: wip_vasikaran
1 Replies

8. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

9. Shell Programming and Scripting

Replace a Directory path with another in a file

Hi, I have abt 20-30 scripts using a directory structure. I have to replace a directory structure in a file for example "/i01/proc" with "/dwftp/scripts" using sed. The command :- cat filename | sed 's/"i01/proc"/"dwftp/scripts"' is not working. Can someone help me in this regard? ... (3 Replies)
Discussion started by: venkatajay_18
3 Replies

10. UNIX for Dummies Questions & Answers

home directory in search path

Is it unsafe to put your own home directory (a regular user) in your search path? I am writing useful shell scripts, but don't have the permissions to put them in /usr/bin. (Korn shell) thanks (2 Replies)
Discussion started by: jpprial
2 Replies
Login or Register to Ask a Question