0

I am using following kind of script in my perl script and expecting entry at line 1. I am getting some error as below; any help?

plz ignore perl variable....

Error Messaage - sed: -e expression #1, char 22: extra characters after command

# Empty file will not work for Sed line number 1
    `"Security Concerns Report" > $outputFile`;

    `sed '1i\
    \
    DATE :- $CDate \
    Utility accounts with super access:- $LNumOfSupUserUtil \
    Users not found in LDAP with super access: - $LNumOfSupUserNonLdap\

    ' $outputFile > $$`;
    `mv $$ $outputFile`;
}
4
  • 10
    I'm not quite sure why you're using Perl to invoke sed and mv, when Perl can do all of this and more besides natively! Aug 3, 2012 at 13:29
  • 3
    this seems like it should be a bash script... not a perl script Aug 3, 2012 at 13:43
  • basically this IS a way (be it horrible) to write a bash script in Perl ;)
    – pavel
    Aug 3, 2012 at 13:59
  • 1
    What do they teach kids these days? :-) Aug 3, 2012 at 14:34

1 Answer 1

5

Your immediate problem is that the backslash character is interpreted by Perl inside the backtick operator, as is the dollar character. So your backslash-newline sequence turns into a newline in the shell command that is executed. If you replace these backslashes by \\, you'll go over this hurdle, but you'll still have a very brittle program.

Perl is calling a shell which calls sed. This requires an extra level of quoting for the shell which you are not performing. If your file names and data contain no special characters, you may be able to get away with this, until someone uses a date format containing a ' (among many things that would break your code).

Rather than fix this, it is a lot simpler to do everything in Perl. Everything sed and shells can do, Perl can do almost as easily or easier. It's not very clear from your question what you're trying to do. I'll focus on the sed call, but this may not be the best way to write your program.

If you really need to prepend some text to an existing file, there's a widely-used module on CPAN that already does this well. Use existing libraries in preference to reinventing the wheel. File::Slurp has a prepend_file method just for that. In the code below I use a here-document operator for the multiline string.

use File::Slurp; # at the top of the script with the other use directives
File::Slurp->prepend_file($outputFile, <<EOF);
DATE :- $CDate
Utility accounts with super access:- $LNumOfSupUserUtil
Users not found in LDAP with super access: - $LNumOfSupUserNonLdap
EOF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.