perl's substitution operator "s" with file contents?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl's substitution operator "s" with file contents?
# 1  
Old 02-01-2012
Question perl's substitution operator "s" with file contents?

Please show me how to make substitution over the contents of a file in a perl script.

In a perl script, the core part of substitution operation is

s/FINDPATTERN/REPLACEPATTERN/g;

However, I cannot figure out how to make the substitution occur over the contents of a file. The following perl script failed by syntax error.

Code:
#!/usr/bin/perl
use strict;
use warnings;
s/FINDPATTERN/REPLACEPATTERN/g  FILE;


The substitution over the contents of a file involves reading and writing of the file. I do not know how to connect the substitution to the reading and writing of a file.
# 2  
Old 02-01-2012
If the substitution is the only operation that you want to perform on the file, then you can use this on the command line:
Code:
perl -i -0pe 's/FINDPATTERN/REPLACEPATTERN/g' file

# 3  
Old 02-01-2012
Quote:
Originally Posted by bartus11
If the substitution is the only operation that you want to perform on the file, then you can use this on the command line:
Code:
perl -i -0pe 's/FINDPATTERN/REPLACEPATTERN/g' file



No, my perl script is supposed to grow with more statements. I would like all the operations to be done inside a perl script.
# 4  
Old 02-01-2012
Quote:
Originally Posted by LessNux
...
The substitution over the contents of a file involves reading and writing of the file. I do not know how to connect the substitution to the reading and writing of a file.
Code:
$
$ # display the data file
$ cat -n data_file
    1  this is line 1
    2  this is line 2
    3  i want to substitute this => ~ <= by that => # <=
    4  at multiple places => abc~ def~ghi ~~jkl ~~
    5  in multiple lines => ~
    6  and here ~
    7  and here as well ~
    8  this is line 8
    9  and this is the last line...
$
$ # show the Perl program to update the data file
$ cat -n update_data_file.pl
    1  #!/usr/bin/perl -w
    2  use strict;
    3  my $old = "data_file";      # the data file
    4  my $new = "data_file.new";  # a new/temporary file, which will be moved to $old
    5  open(OLD, "<", $old)      or die "can't open $old: $!";
    6  open(NEW, ">", $new)      or die "can't open $new: $!";
    7  while (<OLD>) {
    8    s/~/#/g;                  # globally replace ~ by # in the current line
    9    print NEW $_;             # print the updated line to new/temporary file
   10  }
   11  close(OLD)                or die "can't close $old: $!";
   12  close(NEW)                or die "can't close $new: $!";
   13  # back up the $old file and rename the new/temporary file to $old
   14  rename($old, "$old.orig") or die "can't rename $old to $old.orig: $!";
   15  rename($new, $old)        or die "can't rename $new to $old: $!";
$
$ # run the Perl program
$ perl update_data_file.pl
$
$ # check the updated data file
$ cat -n data_file
    1  this is line 1
    2  this is line 2
    3  i want to substitute this => # <= by that => # <=
    4  at multiple places => abc# def#ghi ##jkl ##
    5  in multiple lines => #
    6  and here #
    7  and here as well #
    8  this is line 8
    9  and this is the last line...
$
$ # check the backup file
$ cat -n data_file.orig
    1  this is line 1
    2  this is line 2
    3  i want to substitute this => ~ <= by that => # <=
    4  at multiple places => abc~ def~ghi ~~jkl ~~
    5  in multiple lines => ~
    6  and here ~
    7  and here as well ~
    8  this is line 8
    9  and this is the last line...
$
$

tyler_durden

Last edited by durden_tyler; 02-01-2012 at 07:08 PM..
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. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

replace the contents of first column of file "X" with second Column of file "X" in file "Y"

Hi! I am having 02 files. In first file" X" I am having 02 Columns TCP-5100 Sybase_5100 TCP-5600 Sybase_5600 Second file "Y" for example-- :services ( :AdminInfo ( :chkpf_uid ("{A2F79713-B67D-4409-83A4-A90804E983E9}") :ClassName (rule_services) ) :compound ()... (12 Replies)
Discussion started by: shahid1632
12 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

Equivalent to Perl's and Bash's "=~" Operator?

Hello All, Alright, so this is driving me absolutely insane. I can't seem to find this ANYWHERE... I've tried every combination and synonym I can think of for this trying to search Google. What is the Expect/Tcl equivalent to Perl and Bash's "=~" operator, (i.e. the "contains" operator).... (2 Replies)
Discussion started by: mrm5102
2 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

What "-a" operator means in "if" statement

Hi I am trying to figure out what the following line does, I work in ksh88: ] && LIST="$big $LIST" Not sure what "-a" means in that case. Thanks a lot for any advice -A (1 Reply)
Discussion started by: aoussenko
1 Replies

7. UNIX for Dummies Questions & Answers

Script For Deleting Contents of "Live" Log File

In our shop, we have a situation where a log file from our interface engine software has begun maxing out in file size (reaching the 32-bit "2147483647" limit). Currently, the only way to rectify this is to stop the interface and restart it, which generates a new log. Easy enough, but the... (6 Replies)
Discussion started by: rjhjr64
6 Replies

8. Shell Programming and Scripting

error "test: [-d: unary operator expected" very confused.

Im trying to check if a series of directory exists and if not create them, and am having issues. All the instances of test return with the error "test: #!/bin/bash location_Parent=~/Documents/sight_of_sound location_IMG=~/Documents/Sight_of_sound/IMG location_AUD=~/Documents/Sight_of_sound/AUD... (4 Replies)
Discussion started by: orionrush
4 Replies

9. Shell Programming and Scripting

How can I use a pipe operator in a variable: OPTION="| command"?

I have a string of commands I am piping some data through and I want to allow command line switches to select which commands are used. I want to do something like this: OPTION="| command3" command1 -a -b c.txt | command2 -d -e $OPTION >result.txt I want to do it that way because OPTION may be... (1 Reply)
Discussion started by: KenJackson
1 Replies

10. Shell Programming and Scripting

Difference between "Command substitution" and "Process substitution"

Hi, What is the actual difference between these two? Why the following code works for process substitution and fails for command substitution? while IFS= read -r line; do echo $line; done < <(cat file)executes successfully and display the contents of the file But, while IFS='\n' read -r... (3 Replies)
Discussion started by: royalibrahim
3 Replies
Login or Register to Ask a Question