Grep: show only what matched in a regex group [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
How can I grep only what matched in a regex group?
for example, get from:
some text ... <a href='...'/user/9082/>... </a>
only numbers from /user/9082/:
9082
What I've tried:
echo "some text ... <a href='...'/user/9082/>3435435345345</a>" | grep -Eo "/user/([0-9]+)/"

Use sed.
$ echo "some text ... <a href='...'/user/9082/>3435435345345</a>" |
> sed -E 's|^.*/user/([0-9]+)/.*$|\1|'
9082
You say "I can use also sed and other methods" implying you are aware sed is the right tool, but that you don't want to use it. Can you elaborate on why? grep is for searching, sed is for formatting.

You could use a bash regex:
str="some text ... <a href='...'/user/9082/>... </a>"
re="/user/([0-9]+)/"
[[ $str =~ $re ]] && echo ${BASH_REMATCH[1]}

Using grep
echo "some text ... <a href='...'/user/9082/>3435435345345</a>" | grep -o '\/user\/[0-9]\+\/'

Related

Programming environment? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 months ago.
Improve this question
I'm trying to add color to a part of my Ubuntu bash prompt.
This part isn't working: (\e[0;31m\1\e[m)
It works, however, if I place it within the PS1 export.
What am I doing wrong?
show_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\e\[0;31m\1\e\[m) /'
}
export PS1="\[\e]0;\u#\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u#\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$(show_git_branch)\$ "
Let's imagine you asked this question:
Why does putting \e\[0;31m in PS1 variable content result in a colored text output, when putting the same string inside sed s command replacement expression does not result in colored output?
It's because Bash when printing the content of PS1 variable changes the sequence of two characters \ and e by the byte with the value 033 octal or 0x1b hexadecimal. In contrast, for sed the sequence of \ e characters as part of replacement expression is technically invalid and in practice is not interpreted specially and with GNU sed just results in a literal e character on output.
If you want to output the hex byte 0x1b with GNU sed you can use the \x1b escape sequence, or you can rely on Bash ANSI-C quoting to pass the byte literally to sed.
echo a | sed 's/^/\x1b\[31m/'
# or
echo a | sed 's/^/'$'\e''\[31m/'
# the same
echo a | sed 's/^/'$'\E''\[31m/'

List only numerical directory names in linux [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
How to list only numerical directory names in Linux.
Only directories, that has only numeric characters?
There are multiple solutions to do it.
1.You can List just dirs and then remove . and / from the names and then Grep just numerical ones:
ls -d ./*/ | sed 's/\.\///g' | sed 's/\///g' | grep -E '^[0-9]+$'
2.By "ls" & "grep" & then "awk". Just list with details, Grep dirs and then Print 9th column:
ls -llh | grep '^d' | awk '{print $9}'
Good luck In Arbaeen.
In bash, you can benefit from extended globbing:
shopt -s extglob
ls -d +([0-9])/
Where
+(pattern-list)
Matches one or more occurrences of the given patterns
The / at the end limits the list to directories, and -d prevents ls from listing their contents.

What is the equivalent of "grep -e pattern1 -e pattern2 <file> " in Solaris? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
What is the equivalent of grep -e pattern1 -e pattern2 "$file" in Solaris?
In Linux it works fine. but in Solaris, i got "grep: illegal option -- e
Usage: grep -hblcnsviw pattern file . . ." error.
Can anyone help please?
Instead of:
# GNU grep only
grep -e pattern1 -e pattern2 file
...you can use:
# POSIX-compliant
grep -e 'pattern1
pattern2' file

looking for a better way to write a command piping tr in linux [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
I was just wondering if there is anyway to do this command more efficiently than the way I did. If there is please let me know!
cat logfile | tr '[' '(' | tr '{' '(' | tr ']' ')' | tr '}' ')'
tr works by replacing characters (or regular expressions) but handles only one operation per invocation
sed may be more suited to achieve what you want. It can handle multiple operations per invocation
try
sed -e 's/\[/(/g' -e 's/{/(/g' -e 's/\]/)/g' -e 's/}/)/g' catfile
you can even edit the input file with the -i switch to sed
sed -i -e 's/\[/(/g' -e 's/{/(/g' -e 's/\]/)/g' -e 's/}/)/g' catfile
Note:
[ indicates the beginning of a regular expression, so it has to be escaped by a backslash to make sed search and replace the string literal [
You can combine all translations into one
tr '[]{}' '()()'

Is \d not supported by grep's basic expressions? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
Improve this question
This does not generate any output. How come?
$ echo 'this 1 2 3' | grep '\d\+'
But these do:
$ echo 'this 1 2 3' | grep '\s\+'
this 1 2 3
$ echo 'this 1 2 3' | grep '\w\+'
this 1 2 3
As specified in POSIX, grep uses basic regular expressions, but \d is part of a Perl-compatible regular expression (PCRE).
If you are using GNU grep, you can use the -P option, to allow use of PCRE regular expressions. Otherwise you can use the POSIX-specified [[:digit:]] character class in place of \d.
echo 1 | grep -P '\d'
# output: 1
echo 1 | grep '[[:digit:]]'
# output: 1
Try this $ echo 'this 1 2 3' | grep '[0-9]\+'

Resources