-6

I have a list of domains without subdomains in a text file. I need the TLD and SLD removed.

Input

google.uk
example.com
amazon.co.uk
domain.ca.uk
education.edu.it

Expected output:

google
example
amazon
domain
education
12
  • 3
    Does this answer your question? Remove subdomains with awk or sed
    – Archemar
    Jul 29, 2022 at 9:43
  • 6
    How about you read the other answers and explanation and use that knowledge to solve this transfer task by yourself?
    – Philippos
    Jul 29, 2022 at 10:04
  • 4
    I'm voting on the question, not on your secret attempts. If you include your own effort, I can judge the modified question.
    – Philippos
    Jul 29, 2022 at 10:17
  • 2
    “You cannot help people permanently by doing for them, what they could and should do for themselves.” (Abraham Lincoln)
    – Philippos
    Jul 29, 2022 at 10:51
  • 9
    @xorghelpneed actually no, Stack Exchange is absolutely not about giving people the answer they want when they want it. Not even close. It is about collecting a useful library of solutions to common problems, and we expect people to put effort into questions and try to solve it themselves first. The attitude of "I don't have time to waste putting effort to learn something, so I will waste other people's time instead" isn't welcome here.
    – terdon
    Jul 29, 2022 at 11:45

4 Answers 4

5

You don't need awk or sed for this, this is the job that cut exists to do:

$ cut -d'.' -f1 file
google
example
amazon
domain
education
5

I found the answer to my own question. Turns out to be very simple

awk -F. '{print $1}' input_file
  • I use the '.' as seperator with '-F.' like this I put the SLD and TLD in column $2 and/or $3

  • With '{print $1}' I only print text in the first column which is the output I need

1
  • Do be cautious though, as results might be what you think unless the input is already in a very strict subset. If you have www.example.com as input, your answer (and all others answers too) will fail to provide to correct result. This is due to the fact, that in general, you can NOT do what you want with just a regex or a simple cut. You would need to consult the Public Suffix List to really get a list of suffixes and then extract the "registerable domain" first label out of them. Nov 23, 2022 at 15:47
2
sed "s/\..*//" filename

google
example
amazon
domain
education

The \. matches the first literal dot, the .* everything after. They get substituted by nothing, thus everything starting from the first dot gets removed.

1

Using Raku (formerly known as Perl_6)

~$ raku -ne 'put .split(".")[0];'  input_file

Maybe a Raku solution will be helpful: above is a fairly close translation of awk code, except Raku (and Perl) are zero-indexed. Raku's -ne "non-autoprinting linewise" command-line flags are used (for more sed-like behavior, use Raku's -pe command-line flags).

Raku provides fewer command-line switches than Perl, meaning more gets accomplished within the language (increasing code portability). Here the field-separator is defined using split which destructively-removes . from the resultant strings. Then the [0] zero-indexed first element is put (put is short for "print-using-terminator", i.e. a newline is added at the end for you).

Sample Input:

google.uk
example.com
amazon.co.uk
domain.ca.uk
education.edu.it

Sample Output:

google
example
amazon
domain
education

https://raku.org

You must log in to answer this question.

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