Skip to main content
2 of 3
added 5 characters in body
Sonamor
  • 193
  • 1
  • 2
  • 7

Find string while knowing part of it and return string

I am new to linux and bash, so bear with me.

I have a string, for example

"Icecream123 AirplaneBCD CompanyTL1 ComputerYU1"

Let's say I know that my string will contain for sure the substring IceCream but I don't know what follows it.

It might be 123 as in my example or it might be something different.

While I can use grep to detect if "Icecream" substring exists in my string with the following command

echo $string | grep -oF 'Icecream';

Which will print

Icecream

I want with a command to get it to print the whole substring, which in my example is

Icecream123

Of course what follows Icecream is random and not known beforehand so I can't just do

$SUBSTRING=$(echo $string | grep -oF 'Icecream')
$SUBSTRINGTRAIL=123
echo $SUBSTRING$SUBSTRINGTRAIL
Sonamor
  • 193
  • 1
  • 2
  • 7