ext: Bash Oneliner Sed Awk
Find
for file in $(find . -name "*.txt"); do
# Do something
done
# Or
find "$dir" -type f | while read -r FILE; do
# Do something
done
## Or
# {} is a placeholder for the filename
# + tells it to include as many paths as possible
# Alternatively, use \; to say to use only one file per invocation
find . -name "*.csv" -exec command {} +
CSV
# the second sort identifies the second field (-k 2) and sorts by that
echo $PATH | cut -d / -f 2 | sort | uniq -c | sort -k 2
xargs
Runs a command against each individual output
# -n 1 -- only one arg
# -P10 parallelize with 10 threads
# -I{} use the arg with a placeholder
# -o save the output to placeholder.csv
seq 0 1000 10000 | xargs -n 1 -P10 -I{} curl "site.org/file.csv/{}" -o {}.csv
Basic Commands
# translate char to another
tr ':' '\n'
# select fields out of delimiters
# select the second field (-f) out of the / delimiters
cut -d / -f 2
# identify the number of unique fields and the count of them
uniq -c
#! /bin/bash
var=3
echo "User: $var"
echo hell{o,yeah}
# Replace a command with its output
echo $(date)
# Check the number of args
if [[ $# -lt 2 || $# -eq 4 ]]; then
fi
for val in list; do
done
for x; do # defaults to in $@
done
Arg parsing
$@ |
Args |
$# |
number of params |
$k |
k-th positional command line param |
-e |
File exists |
-d |
File exists and is a directory |
-r |
file is readable |
-w |
writeable |
-x |
executable |
-s |
size > 0 |
-z STRING |
length 0 |
-n |
> 0 |
== |
equality |
Functions