I have the text SAR_aPP.D7HG32689 , and I only want to get the last 4 characters, in this case it would be 2689 , but it can be something else, since the text is random and the total number of characters can also vary.
How can I get this with sed
?
I tried with:
$ sed 's/SAR_aPP.//g'
To remove the first part, but what follows I have no idea.
I tried with: cut -c
, but I could only see that it strips characters from front to back, or it won't use it correctly.
You can use the following regular expression:
(.{4})$
, which encloses in a group those strings that have four characters and that are before reaching the end of the line.Wearing
sed
Its use with
sed
is:Where it
-r
enables the use of extended regular expressions, and the metacharacter\1
indicates that the match (the four-digit string) is to be displayed; the rest not.Example generating 2 random numbers of 5 digits:
Wearing
grep
But you can also with
grep -Po '.{4}$'
:Wearing
cut
You can use a workaround with
cut
:Here I used
rev
in each row to reverse the order. Thus,cut
he can now take the first four (which were the last), and then he invests those four again with anotherrev
.with the shell
You can also use the shell tools:
In Bash or in Zsh: