HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Using sed regular expression to extract domain name from file

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
expressionfileregularsednameusingextractfromdomain

Problem

I'm learning regex with sed to extract the last field from file named "test". The method I'm trying gives desired output.
Please suggest if this method Im trying is effective way of doing it. Also when should we use "-e" option with sed (please give an example — I couldn't find examples)

~# ] cat test
example.com. 4 IN NS b.iana-servers.net.
50times.com. 21556 IN NS ns1.50times.com.
example.com. 4 IN NS a.iana-servers.net.

~# ] cat test | sed -r 's/^[[:alnum:]].[[:alnum:]].?[a-z].[[:blank:]]+[0-9]+[[:blank:]]+IN[[:blank:]]+[A-Z]+[[:blank:]]+//g' | sed -r 's/\..$//'
b.iana-servers.net
ns1.50times.com
a.iana-servers.net

Solution

When processing tabular data in columns, awk is often a more appropriate tool to use. The equivalent command would be

awk '{ sub("\.$", "", $NF); print $NF }' test


… which I think is more readable.

Explanation:

  • NF is the number of fields: for this text, 5.



  • $NF is the content of the last (5th) field.



  • sub("\.$", "", $NF) strips the trailing dot from the last field.



  • { commands } executes the commands for every line in the file.

Code Snippets

awk '{ sub("\.$", "", $NF); print $NF }' test

Context

StackExchange Code Review Q#97096, answer score: 3

Revisions (0)

No revisions yet.