Skip to content

Instantly share code, notes, and snippets.

@ThinGuy
Last active October 7, 2018 00:56
Show Gist options
  • Save ThinGuy/b456786da16a142e896ad8e0b0652aab to your computer and use it in GitHub Desktop.
Save ThinGuy/b456786da16a142e896ad8e0b0652aab to your computer and use it in GitHub Desktop.
small function to create n spaces between words. Useful with utils like sed/awk/cat
sp() { printf "%0.0s " $(seq 1 $1); }
Example:
# Say you need to add two lines near the end of /etc/security/limits.conf
Orig file:
#<domain> <type> <item> <value>
#
#* soft core 0
#root hard core 100000
# End of file
Desired file:
#<domain> <type> <item> <value>
#
#* soft core 0
#root hard core 100000
* soft nofile 100000
* hard nofile 100000
# End of file
# Instead of manually putting spaces in using sed like so:
sed '$i* soft nofile 100000\n\* hard nofile 100000' /etc/security/limits.conf
# You can use this function to create the spaces
sed "\$i\*$(sp 16)soft$(sp 4)nofile$(sp 10)100000\n\*$(sp 16)hard$(sp 4)nofile$(sp 10)100000" /etc/security/limits.conf
# Note if using single quotes with sed, you'll have to put double quotes within the single qutoes when calling the function.
# WHile it results in a longer command, the precision is arguable better than manually spacing the words out.
sed '$i*'"$(sp 16)"'soft'"$(sp 4)"'nofile'"$(sp 10)"'100000\n*'"$(sp 16)"'hard'"$(sp 4)"'nofile'"$(sp 10)"'100000' /etc/security/limits.conf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment