Author Topic: Need some grep help, please [Solved]  (Read 262 times)

Offline satuser083

  • Hero Member
  • *****
  • Posts: 510
Need some grep help, please [Solved]
« on: December 04, 2012, 08:22:42 AM »
I need some help with using grep in a script. In the script I have

arc_src_UUID1=`ls -l /dev/disk/by-label | grep "$arc_src_label"`, where arc_src_label contains e.g. "Abcde"

Grep finds all occurences of "Abcde", including "Abcde", "Abcdef", "Abcdefg", etc., whereas I only want "Abcde". Adding a space to the contents of arc_src_label has no effect (it looks as though grep ignores this space).

In a terminal the following works

ls -l /dev/disk/by-label | grep "Abcde ", i.e. embedding the pattern-string (plus a space) in "" solves the problem - only "Abcde" is found. Since this clearly works, I assume that it's a script-syntax problem.

Can anyone help me out with the correct syntax? Thanks.
« Last Edit: December 04, 2012, 08:45:33 AM by satuser083 »

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 11057
  • MLUs Forever!
Re: Need some grep help, please
« Reply #1 on: December 04, 2012, 08:39:32 AM »
Can you not add the space when 'Abcde' is applied to the variable?

arc_src_label="Abcde "

.....  or maybe the  grep  -w  option would do as you want?

grep -w Abcde

« Last Edit: December 04, 2012, 08:45:07 AM by Just17 »
MLUs rule the roost!

Linux XPS 3.4.48-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline satuser083

  • Hero Member
  • *****
  • Posts: 510
Re: Need some grep help, please
« Reply #2 on: December 04, 2012, 08:45:06 AM »
I need some help with using grep in a script. In the script I have

arc_src_UUID1=`ls -l /dev/disk/by-label | grep "$arc_src_label"`, where arc_src_label contains e.g. "Abcde"

Grep finds all occurences of "Abcde", including "Abcde", "Abcdef", "Abcdefg", etc., whereas I only want "Abcde". Adding a space to the contents of arc_src_label has no effect (it looks as though grep ignores this space).

In a terminal the following works

ls -l /dev/disk/by-label | grep "Abcde ", i.e. embedding the pattern-string (plus a space) in "" solves the problem - only "Abcde" is found. Since this clearly works, I assume that it's a script-syntax problem.

Can anyone help me out with the correct syntax? Thanks.


OK, should have looked a bit longer  :( . The answer I found in a tutorial is:

... grep -w "$arc_src_label"`, although the logic of the -w in this case isn't immediately apparent (not to me, anyway). Important thing, though,  it's working now.

Offline pags

  • Hero Member
  • *****
  • Posts: 2602
  • Keep it clean.
Re: Need some grep help, please
« Reply #3 on: December 04, 2012, 09:36:52 AM »
I need some help with using grep in a script. In the script I have

arc_src_UUID1=`ls -l /dev/disk/by-label | grep "$arc_src_label"`, where arc_src_label contains e.g. "Abcde"

Grep finds all occurences of "Abcde", including "Abcde", "Abcdef", "Abcdefg", etc., whereas I only want "Abcde". Adding a space to the contents of arc_src_label has no effect (it looks as though grep ignores this space).

In a terminal the following works

ls -l /dev/disk/by-label | grep "Abcde ", i.e. embedding the pattern-string (plus a space) in "" solves the problem - only "Abcde" is found. Since this clearly works, I assume that it's a script-syntax problem.

Can anyone help me out with the correct syntax? Thanks.


OK, should have looked a bit longer  :( . The answer I found in a tutorial is:

... grep -w "$arc_src_label"`, although the logic of the -w in this case isn't immediately apparent (not to me, anyway). Important thing, though,  it's working now.


From
Code: [Select]
man grep

Quote
-w, --word-regexp
              Select  only  those  lines  containing  matches  that  form  whole words.  The test is that the matching
              substring must either be at the beginning of the line, or preceded by a non-word constituent  character.
              Similarly,  it  must  be  either at the end of the line or followed by a non-word constituent character.
              Word-constituent characters are letters, digits, and the underscore.

So, it is only matching when the string you're looking for is a "whole word"

Offline satuser083

  • Hero Member
  • *****
  • Posts: 510
Re: Need some grep help, please [Solved]
« Reply #4 on: December 04, 2012, 10:41:48 AM »
Thanks, pags, knew the explanation was there somewhere. I was pretty sure there was an option for this, but couldn't find it in the million or so hits I got about grep-syntax  ;). Thanks, once again  :) :)