-1

Im trying to use sed command in order to find an error in my text file and replace it with something else.

like i said before sed works perfectly for me without using double quotes in the text, also this -> ~ doesnt work like you suggested, unless i used it wrong. The Perl script

#!/usr/bin/perl

use strict;
use warnings;
my @array = (
"^OPTIONS.*\-X",
"^OPTIONS.*\-x"
);

my $Replace_To = 'OPTIONS="-u hello:bye -p /var/run/myfile.pid -g"'
chomp $Replace_To;
my strCommand;
my $array;

sub main
{
   my $hostname = `hostname -s`;
   chomp $hostname;
   foreach $array(@array)
   {
       my $execute = `awk '/$array/' /tmp/file.txt
       if($execute)
       {
          chomp $execute;
          $strCommand = `sed -i 's/$execute/$Replace_To/g' /tmp/file.txt`;
       }
   }

}
&main();
Tal
  • 25
  • 5
  • 1
    You implemented sed in perl? Like the old [psed](https://metacpan.org/pod/App::s2p)? Nice. – Shawn Nov 20 '19 at 09:38
  • (Assuming OP is _calling_ `sed` _from_ Perl.) There’s no reason to use `sed` in Perl. Perl can do all `sed` can. – Biffen Nov 20 '19 at 10:10
  • As for the error it looks like (it would be nice to see the actual code, BTW) you’re trying to use a variable within the `sed` command but the variable contains slashes, in which case see [How to pass a variable containing slashes to sed](https://stackoverflow.com/questions/27787536/how-to-pass-a-variable-containing-slashes-to-sed). Or just use plain Perl. – Biffen Nov 20 '19 at 10:13
  • @Tal Your question is unclear. Are you _implementing_ `sed` in Perl or are you _using_ it? Please show some code, preferably a [mcve]. And the title doesn’t quite match the rest of the question. – Biffen Nov 20 '19 at 10:15
  • Thank you for your reply, forget about perl for a sec, i'm trying to run that command from shell itself: sed -i 's/myname="jorge: and im 27 years old -X /tmp/wow/bla -g/myname="Dan: and im 27 years old /tmp/wow/bla -g/g' /tmp/test.txt – Tal Nov 20 '19 at 11:19
  • 1
    @Tal You’re using slashes as delimiters for the `s` command but you’ve got too many slashes. Once agian, see [How to pass a variable containing slashes to sed](https://stackoverflow.com/questions/27787536/how-to-pass-a-variable-containing-slashes-to-sed). – Biffen Nov 20 '19 at 11:22
  • 1
    @Tal Or just write it all in Perl. It’ll be much easier. – Biffen Nov 20 '19 at 11:23
  • @Biffen I've posted the example code – Tal Nov 20 '19 at 11:52
  • @Tal There’s _never_ a need to call `sed` (or `awk`) from Perl. Perl’s text parsing and regex engine are two of its best features. As for putting a variable in a regex; [`quotemeta`](https://perldoc.pl/functions/quotemeta) is your friend. – Biffen Nov 20 '19 at 12:38
  • @Biffen i'm in close env and not all modules / features are available for me. but its ok, i found the solution. thanks anyway :) – Tal Nov 20 '19 at 13:15
  • @Tal `sed`-like line iteration and regexen are at the very core of the Perl language, no extra features or modules required. – Biffen Nov 20 '19 at 13:16

1 Answers1

1

The solution i found for my problem is pretty simple: just had to put \ before every / i had in my text.

Tal
  • 25
  • 5