0

I have a script that searches through a file and displays the results.

However there is a problem for example when i search 1 the following results are given:

1 B C
11 D E
12 B C
13 D E

When i search for 1, I only want it to show the 1 not also

11 D E
12 B C
13 D E

Is this possible?

echo "$@" | sed 's/[[:space:]]/.*/g' | xargs -Ifile grep -Ei 'file' text.txt
user3411002
  • 783
  • 3
  • 9
  • 27

3 Answers3

1

You can use :

grep -w "1" <filename>

Explaination:

-w, --word-regexp
             The expression is searched for as a word

Output without w:

grep "1" abc.txt 
1
12
13
111
123
312
412

Output with w:

grep -w "1" abc.txt 
1

When content of abc.txt is :

1
12
13
111
123
312
412
Pacifist
  • 3,025
  • 2
  • 12
  • 20
0

You can use -w flag with grep command. Please check the screenshot below.

enter image description here

Harijith R
  • 329
  • 2
  • 8
  • Is there a way to run this from a script so that i just call the script such as ./script.sh 1 and it does ths. Ive updated the question to show why i use the expression i did. – user3411002 Sep 25 '19 at 08:49
0

Try this in the script:

grep -o "\b$1\b" <file_name>

Example

vals=1

cat word.txt 
1 B C
11 D E
12 B C
13 D E

grep -o "\b${vals}\b" word.txt 
1
sungtm
  • 547
  • 3
  • 12