To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).
\d for single or multiple digit numbers It will match any single digit number from 0 to 9. \d means [0-9] or match any number from 0 to 9. Instead of writing 0123456789 the shorthand version is [0-9] where [] is used for character range. [1-9][0-9] will match double digit number from 10 to 99.
A regular expression (regex) is a text pattern that can be used for searching and replacing. Regular expressions are similar to Unix wild cards used in globbing, but much more powerful, and can be used to search, replace and validate text.
You can do this with GNU grep's perl mode:
echo "12 BBQ ,45 rofl, 89 lol" | grep -P '\d+ (?=rofl)' -o
echo "12 BBQ ,45 rofl, 89 lol" | grep --perl-regexp '\d+ (?=rofl)' --only-matching
-P
and --perl-regexp
mean Perl-style regular expression.
-o
and --only-matching
mean to output only the matching text.
Yes regex can certainly be used to extract part of a string. Unfortunately different flavours of *nix and different tools use slightly different Regex variants.
This sed command should work on most flavours (Tested on OS/X and Redhat)
echo '12 BBQ ,45 rofl, 89 lol' | sed 's/^.*,\([0-9][0-9]*\).*$/\1/g'
It seems that you are asking multiple things. To answer them:
You can extract the numbers by catching them in capturing parentheses:
.*(\d+) rofl.*
and using $1
to get the string out (.*
is for "the rest before and after on the same line)
With sed as example, the idea becomes this to replace all strings in a file with only the matching number:
sed -e 's/.*(\d+) rofl.*/$1/g' inputFileName > outputFileName
or:
echo "12 BBQ ,45 rofl, 89 lol" | sed -e 's/.*(\d+) rofl.*/$1/g'
you can use the shell(bash for example)
$ string="12 BBQ ,45 rofl, 89 lol"
$ echo ${string% rofl*}
12 BBQ ,45
$ string=${string% rofl*}
$ echo ${string##*,}
45
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With