Skip to content

Latest commit

 

History

History
82 lines (58 loc) · 1.52 KB

unix.file_processing.md

File metadata and controls

82 lines (58 loc) · 1.52 KB

Unix File Processing

Remove all blank lines

grep '.' 

Remove single blank lines

cat -s # Remove single blank lines

Remove leading spaces

create_stream |  sed -e 's/^[ \t]*//' | consume_stream

Remove everything after first space

create_stream |  grep -o '^\S*' | consume_stream

Pretty Print XML

echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' | \
    python -c 'import sys;import xml.dom.minidom;s=sys.stdin.read();print xml.dom.minidom.parseString(s).toprettyxml()'

Remove empty lines

create_stream | awk 'NF' | consume_stream

grep

find . -exec grep -i "hi rik" '{}' /dev/null \; -print

Use sudo with password as parameter

The -S switch makes sudo read the password from STDIN. This means you can do:

echo mypassword | sudo -S command

Reading configs in Bash

configExample.sh

# Try to read the configuration from:
#   1. the working directory
#   2. The user's home directory
# (in that order)
config_file=".slack.conf"
if [ -f "$(pwd)/$config_file" ]; then
  source "$(pwd)/$config_file"
elif [ -f "$HOME/$config_file" ]; then
  source "$HOME/$config_file"
fi

~/.slack.conf

Configuration file (~/.slack.conf) is of the following format:

# Your Slack hook URL
SLACK_HOOK_URL=https://hooks.slack.com/services/BEEF4UP4/BEEF2830/BEEFnA8zI4lbaMjR6zQ8FoWr