One Liners

From NBSWiki
Jump to: navigation, search

These are a collection of "one-liners" that I fond myself retyping a little too often:

nslookup reformatting

Nslookup gives out a format which is annoying to parse and consolidate. For example:

kyron@KyronMB-15 ~/SafeTemp $ nslookup www.google.com
Server:		9.21.38.45
Address:	9.21.38.45#53

Non-authoritative answer:
www.google.com	canonical name = www.l.google.com.
Name:	www.l.google.com
Address: 173.194.77.147
Name:	www.l.google.com
Address: 173.194.77.105
Name:	www.l.google.com
Address: 173.194.77.103
Name:	www.l.google.com
Address: 173.194.77.99
Name:	www.l.google.com
Address: 173.194.77.106
Name:	www.l.google.com
Address: 173.194.77.104

So, to make things more legible, I often end up using AWK to reformat things a little. the key in this case is to use a state variable (hn) to make sure it's the NAME's address and not the SERVER's address we are printing out.

kyron@KyronMB-15 ~/SafeTemp $ for I in www.google.com www.migrosoft.com www.neuralbs.com; do nslookup $I; done | awk '/Name/ {hn=$NF}; /Add/ && hn != "" {print $NF"\t: "hn; hn=""}'
173.194.77.105	: www.l.google.com
173.194.77.106	: www.l.google.com
173.194.77.99	: www.l.google.com
173.194.77.147	: www.l.google.com
173.194.77.103	: www.l.google.com
173.194.77.104	: www.l.google.com
84.16.80.38	: migrosoft.com
65.92.6.203	: kyron.neuralbs.com

Variable transformations

Converting numbered variables into vectors. In other words, variables such as PS1, PS2, PS3, ... will become ps[1], ps[2], ps[3], ...

function f(){ while IFS== read -r var val; do [[ $var = \#* ]] && continue ; [[ $var =~ (.*)([0-9]+)$ ]] && eval "$(echo ${BASH_REMATCH[1]} | tr 'A-Z' 'a-z' )[${BASH_REMATCH[2]}]"=\$val ; done ; } < <(printf "V1=a\nV2=b\nV3=c\n") ; f; echo ${v[@]}
a b c

Now used to parse a file containing variables which is usually sourced.

{ while IFS== read -r var val
do
	[[ $var = \#* ]]           && continue # skip comment lines
	[[ $var =~ (.*)([0-9])$ ]] && eval "$(echo ${BASH_REMATCH[1]}|tr 'A-Z' 'a-z')[${BASH_REMATCH[2]}]"=$val
done ; } < "$conf"
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox