Reputation: 17
Let's say we have a string "test123"
in a text file.
How do we cut out "test12"
only or let's say there is other garbage behind "test123"
such as test123x19853
and we want to cut out "test123x"
?
I tried with grep -a "test123.\{1,4\}" testasd.txt
and so on, but just can't get it right.
I also looked for example, but never found what I'm looking for.
Upvotes: 0
Views: 1038
Reputation: 195039
expr:
kent$ x="test123x19853"
kent$ echo $(expr "$x" : '\(test.\{1,4\}\)')
test123x
Upvotes: 1
Reputation: 133458
If you are ok with awk
then try following(not this will look for continuous occurrences of alphabets and then continuous occurrences of digits, didn't limit it to 4 or 5).
echo "test123x19853" | awk 'match($0,/[a-zA-Z]+[0-9]+/){print substr($0,RSTART,RLENGTH)}'
In case you want to look for only 1 to 4 digits after 1st continuous occurrence of alphabets then try following(my awk
is old version so using --re-interval
you could remove it in case you have latest version of ittoo).
echo "test123x19853" | awk --re-interval 'match($0,/[a-zA-Z]+[0-9]{1,4}/){print substr($0,RSTART,RLENGTH)}'
Upvotes: 0
Reputation: 5252
What you need is -o
which print out matched things only:
$ echo "test123x19853"|grep -o "test.\{1,4\}"
test123x
$ echo "test123x19853"|grep -oP "test.{1,4}"
test123x
-o, --only-matching show only the part of a line matching PATTERN
Upvotes: 0