12

I found this but it assumes the words are space separated.

result="abcdefADDNAME25abcdefgHELLOabcdefgADDNAME25abcdefgHELLOabcdefg"

for word in $result
do
    if echo $word | grep -qi '(ADDNAME\d\d.*HELLO)'
    then
        match="$match $word"
    fi
done

POST EDITED

Re-naming for clarity:

data="abcdefADDNAME25abcdefgHELLOabcdefgADDNAME25abcdefgHELLOabcdefg"
for word in $data
do
    if echo $word | grep -qi '(ADDNAME\d\d.*HELLO)'
    then
        match="$match $word"
    fi
done
echo $match

Original left so comments asking about result continue to make sense.

4
  • I'm having trouble making sense of your script. Is $world supposed to correspond to $result? There's nothing in your pattern that will match anything in $world however. Can you show a better example of the string you're trying to match and the pattern you're trying to use? Sep 4, 2010 at 18:31
  • I edited the post, it was incorrectly stated. Sep 4, 2010 at 19:55
  • It's still not clear what result you're looking for. Now for word in $result only sees one "word" (the full string contained in $result. What do you want $match to contain at the end? Sep 4, 2010 at 20:15
  • I want to extract all occurences of the regex pattern inside the var "result". The var "match" should contain all the extracted matches each separated by a space. Sep 4, 2010 at 20:19

3 Answers 3

18

Use grep -o

-o, --only-matching show only the part of a line matching PATTERN

1
  • 5
    7 years later this was exactly what I needed
    – adg
    Aug 20, 2017 at 19:28
16

Edit: answer to edited question:

for string in "$(echo $result | grep -Po "ADDNAME[0-9]{2}.*?HELLO")"; do
  match="${match:+$match }$string"
done

Original answer:

If you're using Bash version 3.2 or higher, you can use its regex matching.

string="string to search 99 with 88 some 42 numbers"
pattern="[0-9]{2}"
for word in $string; do
  [[ $word =~ $pattern ]]
  if [[ ${BASH_REMATCH[0]} ]]; then
    match="${match:+$match }${BASH_REMATCH[0]}"
  fi
done

The result will be "99 88 42".

5
  • I edited my post: My string does not have spaces, therefore it will not work. Sep 4, 2010 at 20:04
  • 1
    why not shorten it a bit: ... do; [[ $word =~ $pattern ]] && match="${match:+match }${BASH_REMATCH[0]}"; done
    – user377178
    Oct 14, 2013 at 11:20
  • @user377178: It's a matter of style choice and readability. Choose what works best for a particular circumstance. Oct 14, 2013 at 20:32
  • 1
    Why the parameter substitution with the variable "match"? I don't understand the purpose of it.
    – Akito
    May 1, 2020 at 20:53
  • @Akito: There's a space at the end of the parameter substitution. The result is that a space is added to the value of match between each value. Jun 28, 2022 at 16:09
0

Not very elegant - and there are problems because of greedy matching - but this more or less works:

data="abcdefADDNAME25abcdefgHELLOabcdefgADDNAME25abcdefgHELLOabcdefg"
for word in $data \
    "ADDNAME25abcdefgHELLOabcdefgADDNAME25abcdefgHELLOabcdefg" \
    "ADDNAME25abcdefgHELLOabcdefgADDNAME25abcdefgHELLO"
do
    echo $word
done |
sed -e '/ADDNAME[0-9][0-9][a-z]*HELLO/{
        s/\(ADDNAME[0-9][0-9][a-z]*HELLO\)/ \1 /g
        }' |
while read line
do
    set -- $line
    for arg in "$@"
    do echo $arg
    done
done |
grep "ADDNAME[0-9][0-9][a-z]*HELLO"

The first loop echoes three lines of data - you'd probably replace that with cat or I/O redirection. The sed script uses a modified regex to put spaces around the patterns. The last loop breaks up the 'space separated words' into one 'word' per line. The final grep selects the lines you want.

The regex is modified with [a-z]* in place of the original .* because the pattern matching is greedy. If the data between ADDNAME and HELLO is unconstrained, then you need to think about using non-greedy regexes, which are available in Perl and probably Python and other modern scripting languages:

#!/bin/perl -w
while (<>)
{
    while (/(ADDNAME\d\d.*?HELLO)/g)
    {
        print "$1\n";
    }
}

This is a good demonstration of using the right too for the job.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.