6

Can someone please help me out with what I am doing wrong here, been scratching my head for a bit. I don't know much perl just making a small change to a irssi script.

            $selectedname = <STDIN>;
            $Nameslist = `grep -i -w '$selectedname'~/file`;

It is working correctly the issue I am having is it looks like the variable isn't being inserted correctly. For example if I type in the same command using the word test in the command line I get 3 lines that show up. But if I use the same word with this script it comes up empty.

I know it is working correctly because I can do * as an input and it will show me the whole file. It is just not working with the inputed strings.

Any help would be much appreciated this has really been stumping me.

3
  • 4
    Lines from STDIN are followed by newlines, you can strip them off using the chomp function: perldoc -f chomp Commented Jun 2, 2016 at 19:55
  • 1
    Thats worked perfect, thank you. Commented Jun 2, 2016 at 20:12
  • Since single quotes are being used around the variable, shouldn't the variable not be interpolated? Commented Jun 3, 2016 at 6:12

1 Answer 1

1

Actually when you are using <STDIN>, a new line character is coming. so you need to remove the new line character. you can use chomp to remove the new line character

Code:

   $selectedname = <STDIN>;
   chomp($selectedname);
   $Nameslist = `grep -i -w '$selectedname'~/file`;
Sign up to request clarification or add additional context in comments.

Comments

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.