Replace text in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace text in a file
# 1  
Old 04-11-2016
Replace text in a file

I wrote a program using Perl to find and replace a text within a file.

The text that needs to be replaced in the file is 'sql7.0.1' with 'sqls715'. When I execute my program I get an error message:

Quote:
can't /tmp/odbc.ini_old : A file or directory in the path name does not exist
Here is my code:

Code:
 
#!/usr/bin/perl

use strict;
use warnings;

my $filename = '/tmp/odbc.ini.2014_old';
my $find=' sql7.0.1 ';
my $replace = ' sqls715  ';

{
   local @ARGV =($filename);
   local $^I ='.bac';
   while (<>) {
     if (s/$find/$replace/ig) {
}
 else {
   print;
   }
 }
}
print "Finished";

Moderator's Comments:
Mod Comment Moved from AIX forum to "Shell Programming and Scripting"

Last edited by Scrutinizer; 04-11-2016 at 10:42 PM.. Reason: missing letter in replace
# 2  
Old 04-11-2016
Hello dellanicholson,

I think error message is self explanatory itself, could you please check that file named odbc.ini_old is really present in your box at /tmp or not, most probably it is not, in case it is present then we could sense that it could be an another issue.

Thanks,
R. Singh
# 3  
Old 04-12-2016
Did check the path and it is not the case.
# 4  
Old 04-12-2016
Is $filename name /tmp/odbc.ini.2014_old or /tmp/odbc.ini_old?

The code shown above will remove the line matching the string. For replacing string try updating to:
Code:
#!/usr/bin/perl

use strict;
use warnings;

my $filename = '/tmp/odbc.ini.2014_old';
my $find=' sql7.0.1 ';
my $replace = ' sqls715 ';

{
   local @ARGV =($filename);
   local $^I ='.bac';
   while (<>) {
      if (s/$find/$replace/ig) {
         print;
      } else {
         print;
      }
   }
}
print "Finished\n";

(after the $filename is verified)
This User Gave Thanks to rdrtx1 For This Post:
# 5  
Old 04-12-2016
Replace text in a file

$filename name /tmp/odbc.ini.2014_old is correct

The error message is:

Code:
can't /tmp/odbc.ini.2014_old : A file or directory in the path name does not exist

# 6  
Old 04-12-2016
try:
Code:
#!/usr/bin/perl

use Cwd(realpath);

my $filename = "/tmp/odbc.ini.2014_old";
my $find=' sql7.0.1 ';
my $replace = ' sqls715 ';
{
   local @ARGV =($filename);
   local $^I ='.bac';
   while (<>) {
      if (s/$find/$replace/ig) {
         print;
      } else {
         print;
      }
   }
}
print "Finished\n";

This User Gave Thanks to rdrtx1 For This Post:
# 7  
Old 04-13-2016
Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script to replace text file from a lookup file

Hi. I need assistance with the replacing of text into a specific file via a bash script. My bash script, once run, currently provides a menu of computer names to choose.The script copies onto my system various files, depending what computer was selected in the menu.This is working OK. Now, I... (1 Reply)
Discussion started by: jonesn2000
1 Replies

2. Windows & DOS: Issues & Discussions

2 Questions: replace text in txt file, add text to end of txt file

so... Lets assume I have a text file. The text file contains multiple "#" symbols. I want to replace all thos "#"s with a STRING using DOS/Batch I want to add a certain TEXT to the end of each line. How can I do this WITHOUT aid of sed, grep or anything linux related ? (1 Reply)
Discussion started by: pasc
1 Replies

3. Shell Programming and Scripting

Search and replace from file in awk using a 16 bit text file

Hello, Some time ago a helpful awk file was provided on the forum which I give below: NR==FNR{A=$0;next}{for(j in A){split(A,P,"=");for(i=1;i<=NF;i++){if($i==P){$i=P}}}}1 While it works beautifully on English and Latin characters i.e. within the ASCII range of 127, the moment a character beyond... (6 Replies)
Discussion started by: gimley
6 Replies

4. Shell Programming and Scripting

Replace text in column1 of a file matching columns of another file

Hi all, I have 2 files: species-names.txt Abaca-bunchy-top-virus ((((Abaca-bunchy-top-virus((Babuvirus((Unassigned((Nanoviridae((Unassigned)))) Abutilon-mosaic-virus ((((Abutilon-mosaic-virus((Begomovirus((Unassigned((Geminiviridae((Unassigned))))... (2 Replies)
Discussion started by: thienxho
2 Replies

5. Shell Programming and Scripting

Replace char on text file

Hi , I have problem on replace char on text file #!/bin/bash echo "Enter Third value : " read a sed '2 s/192.160.1.1/cut -d"." -f3/$a/g' tcpip.txt > a.txt I want replace line 2 on tcpip.txt with value a that I input but it's not run Please help me (2 Replies)
Discussion started by: phillipss
2 Replies

6. Shell Programming and Scripting

How to replace 0a with 0d0a in the text file?

Hello, I must replace all occurences of 0a character in a text file with 2 characters 0d0a. I've tried sed, but it cannot insert control characters ie. \n \r - it simply writes \n \r into output file. What is the good solution for my problem? I use ksh and AIX 5.3 operating system. Tia Yac (8 Replies)
Discussion started by: Yac
8 Replies

7. Shell Programming and Scripting

How to replace text in a file with text entered

I am trying to write a shell script that will allow the typing of a value, then using that value to replace data in a text file. I suspect I need sed. The format of the file is: Variable1:Value1 Variable2:Value2 The interaction would be something like: Shell Prompt: "Please enter the... (9 Replies)
Discussion started by: cleanden
9 Replies

8. UNIX for Dummies Questions & Answers

how can search a String in one text file and replace the whole line in another file

i am very new to UNIX plz help me in this scenario i have two text files as below file1.txt name=Rajakumar. Discipline=Electronics and communication. Designation=software Engineer. file2.txt name=Kannan. Discipline=Mechanical. Designation=CADD Design Engineer. ... (6 Replies)
Discussion started by: kkraja
6 Replies

9. UNIX for Dummies Questions & Answers

search and replace a specific text in text file?

I have a text file with following content (3 lines) filename : output.txt first line:12/12/2008 second line:12/12/2008 third line:Y I would like to know how we can replace 'Y' with 'N' in the 3rd line keeping 1st and 2nd lines same as what it was before. I tried using cat output.txt... (4 Replies)
Discussion started by: santosham
4 Replies

10. Shell Programming and Scripting

Replace text in input file

I wish to replace values of specific parameters in an input file for batch runs of a java code. It's essentially a nested for-loop sorta like this: valuearray1 contains values for param1 valuearray2 contains values for param2 for (all values in valuearray1) go into specific position in... (2 Replies)
Discussion started by: daphantomica
2 Replies
Login or Register to Ask a Question