Use grep with regular expression to extract specific substring (-o, –only-matching)By neokrates, written on July 7, 2010 |
bash snippet |
- neokrates
- Email: uwarov@yahoo.com
- Website: http://www.thinkplexx.com
- Join date: 05-31-09
- Posts: 20
Rate it
Ad
Poll
Estimate your experience level with Virtualization technology?
- No experience (100%, 4 Votes)
- Guru (0%, 0 Votes)
- Expert / Admin (0%, 0 Votes)
- Professional / Developer (0%, 0 Votes)
- User (0%, 0 Votes)
- Beginner (0%, 0 Votes)
- Have tried (0%, 0 Votes)
Total Voters: 4
Loading ...
Most popular search terms:
I have a file with some random text and I wish a very specific part of it to be assigned to my bash variable.
File:
$ cat grepme.txt My name is &&&neokrates3223 My name is neokrates-- My name is tadada Ma name is not neokrites
And I wish to know the name.
Suppose, the correct name can be identified:
it is prefixed by “My name is”;
it is surrounded by &&& and some numbers;
I use some grep with pipe to extract the name:
MY_NAME=$(grep -o "My name is [\&\/a-zA-Z0-9\-]*" grepme.txt |grep -o "&[\&\/a-zA-Z0-9\-]*" | grep -o "[a-zA-Z]*" ; echo -e "\n\nYour name is $MY_NAME\n\n"
I get:
Your name is neokrates
Have fun
|
TAGS
|
|
RELATED
|
Pages
Posts
|
|
SOCIAL
|



















there is no closing bracket. also im fairly sure the output of grep -o cannot be piped to another grep – at least its not working for me….
Like or Dislike:
0
0
Hi Blake,
you are right about bracket, publishing problem I guess.
Grep can be generally piped. What Version of grep and under which Os fails to pipe “grep -o“?
Like or Dislike:
0
0