0

Say I have a string such as

="/home/Rob/.SpaceVim"

and I want to extract the string following the dot. How do I do this?

I know $ is also used to represent the last character, and \ can be used to make character in string literal. So I've tried stuff like awk '\.$' but not had any success.

  • 1
    Where do you have this string? In a file, or in a shell variable, or elsewhere? – Kusalananda Oct 18 at 7:33
1

Could be:

echo "/home/Rob/.SpaceVim" | grep -o "\..*$"

or

echo "/home/Rob/.SpaceVim" | sed 's;[^.]*;;'

Without dot and GNU grep

echo "/home/Rob/.SpaceVim" | grep -Po "(?<=\.).*$"

or sed

echo "/home/Rob/.SpaceVim" | sed 's;^.*\.;;'

you could even include cut

echo "/home/Rob/.SpaceVim" | cut -d'.' -f2-

(beware you'll see variations in behaviour among those for lines that contain no dot or more than one dot, but you've not specified what to do in that case).

| improve this answer | |
  • Wonderful!...Now I need to figure out how they work :-| (there was also another solution: echo "${a##*/}" ) Thanks for your help! – 1toneboy Oct 18 at 11:13
  • @1toneboy I suggest to start with the basics of grep, sed and avobe all, regular expressions, which are used in most text manipulation commands. – schrodigerscatcuriosity Oct 18 at 11:16
  • This has been my 'year of linux'; about six months ago I was getting into learning BASH (vim etc.) then due to home demands halted learning. I did to basic awk, grep, sed etc. but I've forgotten the specifics. I remember reading wiki about Regex lol. But yeah, I need to delve back in to really get my head around what's going on. Cheers – 1toneboy Oct 18 at 11:24
  • I meant *above :) – schrodigerscatcuriosity Oct 18 at 11:25
  • I did some BASH research today which was a nice refresher. However, I still can't understand how grep -o "\..*$" AND grep -Po "(?<=\.).*$" work. Also, I know nothing about Perl. For the first one why is there an escape operator then dot \. that's particularly confusing to me. I understand . is single character, wildcard and $ is position after last character in string; but Can you please shed any insight/explanation? Thanks! – 1toneboy Oct 19 at 9:16
-1

Read man bash.

a="/home/Rob/.SpaceVim"
echo ${a##.*/}
| improve this answer | |
  • Don't you mean echo "${a##*/}"? – schrodigerscatcuriosity Oct 18 at 5:40
  • 1
    @schrodigerscatcuriosity Probably more printf '%s\n' "${a##*.}" – Stéphane Chazelas Oct 18 at 7:19
  • @StéphaneChazelas OP referenced in a comment, now deleted, that he wanted (though not very clearly) the output both with dot and without it. I had in mind the former when I did this comment and my answer. – schrodigerscatcuriosity Oct 18 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.