Bash-interesting-command-examples: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
mNo edit summary |
||
| (5 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
====Interesting one-liners==== | ====Interesting one-liners==== | ||
== Find all drives and ignore loop devices == | |||
<pre> | <pre> | ||
root@kvm03:/var/log# lsblk | grep -v "loop\|NAME" | grep "^[a-z]\|^[A-Z]" | awk '{print $1}' | root@kvm03:/var/log# lsblk | grep -v "loop\|NAME" | grep "^[a-z]\|^[A-Z]" | awk '{print $1}' | ||
| Line 15: | Line 15: | ||
</pre> | </pre> | ||
Continue match until match is found | |||
* This is using awk, and seems quite powerful as a tool | |||
* found this little gem at [https://unix.stackexchange.com/questions/21076/how-to-show-lines-after-each-grep-match-until-other-specific-match Stack Exchange] | |||
<pre> | |||
awk '/Word A/,/Word D/' filename | |||
/From/CONTINUE/Until/ | |||
</pre> | |||
== Checking Openstack metadata server == | |||
* useful for routing, connections, and hanging from the server itself | |||
<pre> | |||
for i in {1..20}; do | |||
date; | |||
curl -sS --connect-timeout 2 --max-time 5 -o /dev/null -w 'HTTP=%{http_code} CONNECT=%{time_connect} TOTAL=%{time_total}\n' http://169.254.169.254/openstack/latest/meta_data.json; | |||
sleep 2; | |||
done | |||
</pre> | |||
== Remove non-english directories == | |||
* change the type to f if you are looking for non-english files | |||
* ALWAYS test find results before deleting, duh! | |||
<pre> | |||
sudo find . -type d -not -name "[a-zA-Z0-9]*" -exec rm -rf {} \; | |||
</pre> | |||
== try to make a xml2 replacement for simple XML with xmllint == | |||
Who the hell thought it was a good idea to NOT have xml2 available in both Mac and Linux? What the hell!?!? | |||
* On Mac install gawk via brew | |||
* will leverage xmllint, and then stupidity ensues... WTF? | |||
* never trust this in prod-ish servers without a lot of testing. chatGPT gave this suggestion as a workaround to get the xml2 behavior. | |||
* I am betting that complex xml will make this choke or turn into a pumpkin but meh, it works well enough for simple stuff. | |||
<pre> | |||
echo '<root><name>Chris</name></root>' | xmllint --format - 2>/dev/null | gawk ' | |||
/<[[:alnum:]_:-]+>/ { | |||
tag = gensub(/.*<([[:alnum:]_:-]+)>.*/, "\\1", "g") | |||
path = (path ? path "/" tag : "/" tag) | |||
} | |||
/<\/[[:alnum:]_:-]+>/ { | |||
if ($0 ~ /<[[:alnum:]_:-]+>[^<]+<\/[[:alnum:]_:-]+>/) { | |||
val = gensub(/.*<[[:alnum:]_:-]+>([^<]+)<\/[[:alnum:]_:-]+>.*/, "\\1", "g") | |||
print path "=" val | |||
} | |||
sub(/\/[^/]+$/, "", path) | |||
}' | |||
</pre> | |||
[[Category:Bash]] | [[Category:Bash]] | ||
Latest revision as of 09:03, 1 September 2026
Interesting one-liners
Find all drives and ignore loop devices
root@kvm03:/var/log# lsblk | grep -v "loop\|NAME" | grep "^[a-z]\|^[A-Z]" | awk '{print $1}'
sda
sdb
root@kvm03:/var/log#
root@kvm03:/var/log# lsblk | grep disk | awk '{print $1}'
sda
sdb
root@kvm03:/var/log#
Continue match until match is found
- This is using awk, and seems quite powerful as a tool
- found this little gem at Stack Exchange
awk '/Word A/,/Word D/' filename /From/CONTINUE/Until/
Checking Openstack metadata server
- useful for routing, connections, and hanging from the server itself
for i in {1..20}; do
date;
curl -sS --connect-timeout 2 --max-time 5 -o /dev/null -w 'HTTP=%{http_code} CONNECT=%{time_connect} TOTAL=%{time_total}\n' http://169.254.169.254/openstack/latest/meta_data.json;
sleep 2;
done
Remove non-english directories
- change the type to f if you are looking for non-english files
- ALWAYS test find results before deleting, duh!
sudo find . -type d -not -name "[a-zA-Z0-9]*" -exec rm -rf {} \;
try to make a xml2 replacement for simple XML with xmllint
Who the hell thought it was a good idea to NOT have xml2 available in both Mac and Linux? What the hell!?!?
- On Mac install gawk via brew
- will leverage xmllint, and then stupidity ensues... WTF?
- never trust this in prod-ish servers without a lot of testing. chatGPT gave this suggestion as a workaround to get the xml2 behavior.
- I am betting that complex xml will make this choke or turn into a pumpkin but meh, it works well enough for simple stuff.
echo '<root><name>Chris</name></root>' | xmllint --format - 2>/dev/null | gawk '
/<[[:alnum:]_:-]+>/ {
tag = gensub(/.*<([[:alnum:]_:-]+)>.*/, "\\1", "g")
path = (path ? path "/" tag : "/" tag)
}
/<\/[[:alnum:]_:-]+>/ {
if ($0 ~ /<[[:alnum:]_:-]+>[^<]+<\/[[:alnum:]_:-]+>/) {
val = gensub(/.*<[[:alnum:]_:-]+>([^<]+)<\/[[:alnum:]_:-]+>.*/, "\\1", "g")
print path "=" val
}
sub(/\/[^/]+$/, "", path)
}'