patternMinor
Using sed regular expression to extract domain name from file
Viewed 0 times
expressionfileregularsednameusingextractfromdomain
Problem
I'm learning regex with
Please suggest if this method Im trying is effective way of doing it. Also when should we use
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,
… which I think is more readable.
Explanation:
awk is often a more appropriate tool to use. The equivalent command would beawk '{ sub("\.$", "", $NF); print $NF }' test… which I think is more readable.
Explanation:
NFis the number of fields: for this text, 5.
$NFis 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 }' testContext
StackExchange Code Review Q#97096, answer score: 3
Revisions (0)
No revisions yet.