3

Consider there is a variable line and variable word:

line = 1234 abc xyz 5678
word = 1234

The value of these variables are read from 2 different files.

I want to print the line if it contains the word. How do I do this using shell script? I tried all the suggested solutions given in previous questions. For example, the following code always passed even if the word was not in the line.

if [ "$line"==*"$word"*]; then
    echo $line
fi
3
  • You might want to check out this old question that is one of the highest voted on stackoverflow. stackoverflow.com/questions/229551/string-contains-in-bash Commented Aug 21, 2013 at 7:39
  • possible duplicate of String contains in bash Commented Aug 21, 2013 at 13:09
  • @Kevin: I already tried those answers and did not get the expected result. I have explained this in the question too. Commented Aug 21, 2013 at 17:14

8 Answers 8

4

No need for an if statement; just use grep:

echo $line | grep "\b$word\b"
Sign up to request clarification or add additional context in comments.

Comments

4

You can use if [[ "$line" == *"$word"* ]]

Also you need to use the following to assign variables

line="1234 abc xyz 5678"
word="1234"

Working example -- http://ideone.com/drLidd

Comments

4

Watch the white spaces!

When you set a variable to a value, don't put white spaces around the equal sign. Also use quotes when your value has spaced in it:

line="1234 abc xyz 5678"   # Must have quotation marks
word=1234                  # Quotation marks are optional

When you use comparisons, you must leave white space around the brackets and the comparison sign:

if [[ $line == *$word* ]]; then
    echo $line
fi

Note that double square brackets. If you are doing pattern matching, you must use the double square brackets and not the single square brackets. The double square brackets mean you're doing a pattern match operation when you use == or =. If you use single square brackets:

if [ "$line" = *"$word"* ]

You're doing equality. Note that double square brackets don't need quotation marks while single brackets it is required in most situations.

6 Comments

question: why "single brackets it is required in most situations."?
Ok, got it: [ $a == z* ] # File globbing and word splitting take place.
The double brackets are a special internal test and are not interpolated by the shell first. The single brackets, although they are now a builtin command, stem from the old /bin/test and /bin/[ commands. The statement is fully interpolated by the shell first, and then the test is done. Thus, if line="This is a test", you couldn't do [ $line = "foo" ] because the shell will replace $line and then think there are too many parameters for the test command. However, [[ $line = "foo" ]] is fine.
@DavidW.: Thanks for the detailed explanation on the use of brackets. It worked when I used bash. But why does this does not work with sh?
|
3
echo $line | grep "$word"

would be the typical way to do this in a script, of course it does cost a new process

2 Comments

I get some messages: Usage: grep [OPTION]... PATTERN [FILE]... Try `grep --help' for more information. I get this even after using the '-s' option while using grep.
Could be that $word starts with a hyphen, so it is safer to use grep -- "$word"
3

You can use the bash match operator =~:

 [[ "$line" =~ "$word" ]] && echo "$line"

Don't forget quotes, as stated in previous answers (especially the one of @Bill).

Comments

1

The reason that if [ "$line"==*"$word"* ] does not work as you expect is perhaps a bit obscure. Assuming that no files exist that cause the glob to expand, then you are merely testing that the string 1234 abc xyz 5678==*1234* is non empty. Clearly, that is not an empty string, so the condition is always true. You need to put whitespace around your == operator, but that will not work because you are now testing if the string 1234 abc xyz 5678 is the same as the string to which the glob *1234* expands, so it will be true only if a file named 1234 abc xyz 5678 exists in the current working directory of the process executing the shell script. There are shell extensions that allow this sort of comparison, but grep works well, or you can use a case statement:

case "$line" in
*$word*) echo $line;;
esac

Comments

0

An alternative solution would be using loop:

for w in $line
do
    if [ "$w" == "$word" ]; then
        echo $line
        break
    fi
done

Comments

0

Code Snippet:

$a='text'
$b='text'

if [ $a -eq $b ]
 then
   msg='equal'
fi 

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.