0

In Perl, I'm trying to search a txt file for a string that the user inputs and print out every line that where the string is found.

Here is what I have

my $input = <STDIN>;
print `sed -n "/$input/p" inputfile.txt`;

I'm getting this error: sed -e expression #1, char 9: unterminated address regex

Could anyone help me out with this?

2
  • 2
    The best help would be not to use sed inside Perl. Perl can do that easily. Commented Mar 10, 2015 at 2:56
  • 4
    Also, that might feel like a neat trick to do, but it is very dangerous. You are allowing users to execute arbitrary code on your system. (E.g. use "; rm -rf / as input.) Even if you trust them, things can happen by mistake. Commented Mar 10, 2015 at 3:02

1 Answer 1

4

Don't shell out to sed to do what Perl can do for you.

my $input = <STDIN>;
chomp $input;
open my $fh, '<', 'inputfile.txt' or die $!;
while ( <$fh> ) {
    print if /$input/;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hmm.. I tried this and I'm getting no result when I enter something to search. No errors but no result too.
@JimDavis Yup, you're right lol. Thank you. I'm just learning scripting languages and they're so much more of a pain than learning Java or C++.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.