Answer the question
In order to leave comments, you need to log in
Why can't grep group?
Can anyone explain why grep still can't create groups. Often you want to select a unique piece and replace or display only part of it.
An example of getting an ip address (simplified).
It is logical to think like this:
ifconfig | grep '(inet addr\:)(.*)( B') =$2
What you really have to do is:
(ifconfig | grep inet | cut -d: -f2 | awk '{printf $1"\n"}')
Who can explain the reason for the absence of groups in grep?
Answer the question
In order to leave comments, you need to log in
For starters, you could at least go into man and look at
GREP(1) GREP(1)
NAME
grep, egrep, fgrep - print lines matching a pattern
SYNOPSIS
grep [options] PATTERN [FILE...]
grep [options] [- e PATTERN | -f FILE] [FILE...]
DESCRIPTION
Grep searches the named input FILEs (or standard input if no files are named, or the file name - is given) for lines containing a match to the given PATTERN
. By default, grep prints the matching lines.
In addition, two variant programs egrep and fgrep are available. Egrep is the same as grep -E. Fgrep is the same as grep -F.
It says here that it works with strings, I think that says it all.
Well, if you really don't like doing long pipelines, then learn awk
ifconfig | awk '/inet/{gsub(/.*:/,"",$2);print $2}'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question