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
  • 1
    Where do you have this string? In a file, or in a shell variable, or elsewhere?
    – Kusalananda
    Commented Oct 18, 2020 at 7:33

2 Answers 2

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).

8
  • Wonderful!...Now I need to figure out how they work :-| (there was also another solution: echo "${a##*/}" ) Thanks for your help!
    – 1toneboy
    Commented Oct 18, 2020 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. Commented Oct 18, 2020 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
    Commented Oct 18, 2020 at 11:24
  • I meant *above :) Commented Oct 18, 2020 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
    Commented Oct 19, 2020 at 9:16
-3

Read man bash.

a="/home/Rob/.SpaceVim"
echo ${a##.*/}
3
  • Don't you mean echo "${a##*/}"? Commented Oct 18, 2020 at 5:40
  • 1
    @schrodigerscatcuriosity Probably more printf '%s\n' "${a##*.}" Commented Oct 18, 2020 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. Commented Oct 18, 2020 at 10:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .