Skip to content

Instantly share code, notes, and snippets.

@xnumad
Last active August 3, 2019 14:14
Show Gist options
  • Save xnumad/f3c16b6d360e0aa20910bae517b7e6a9 to your computer and use it in GitHub Desktop.
Save xnumad/f3c16b6d360e0aa20910bae517b7e6a9 to your computer and use it in GitHub Desktop.
Clean dig for CSV (coherent/consistent) parsing

Problem

dig sometimes uses double tabs which is only good for visually viewing lookups of short to medium-length hostnames, but it is bad for reusing the values as in parsing them like a CSV file into a table.

Solution

To prevent the double use of tabs, you can pipe as follows:

dig <host> | sed --quiet 's/[\t ]\+/\t/gp'

Explanation

pipe/forward it to sed and let it replace one or more (greedy) tab or space with a single tab

Thoroughly:

--quiet only output the result
' beginning of script
s  command "substitute"
/ seperator
[\t ]\+  replace one or more (greedy) (the plus char is an escaped quantifier) tab or space
/ seperator
\t with a single tab character
/ seperator
gp  "global (search through every line)" and "print lines" flag
' end of script

Thanks to:

~/.bashrc function/alias

function digc() #dig-clean
{
  dig "$@" | sed --quiet 's/[\t ]\+/\t/gp'
}

Explanation: pass all arguments (i.e. whatever follows after digc) to usual dig, pipe it as described above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment