grep
¶grep
is a standard *NIX tool to search files for patterns and print matching linesgrep
isgrep "todo" /usr/src/linux-headers-4.13.0-25/include/soc/tegra/bpmp-abi.h
grep -R "todo" /usr/src/linux-headers-4.13.0-25/include
grep -Ri "todo" /usr/src/linux-headers-4.13.0-25/include
grep -P "^import" /usr/local/lib/python3.6/dist-packages/jupyter_core/*.py
more src/shell/read_ps_example.sh
grep -vP "^#[^\!]" src/shell/read_ps_example.sh
grep
Output Options¶grep -Ri "todo" /usr/src/linux-headers-4.13.0-25/include
grep -Ri --color=always "todo" /usr/src/linux-headers-4.13.0-25/include | head
grep -Rin --color=always "todo" /usr/src/linux-headers-4.13.0-25/include | head
grep -Ril "todo" /usr/src/linux-headers-4.13.0-25/include | head
grep -Pc "^import" /usr/local/lib/python3.6/dist-packages/jupyter_core/*.py
grep -Ph "^import" /usr/local/lib/python3.6/dist-packages/jupyter_core/*.py | head
grep
has three flags that control this, each taking a numerical argumentgrep -A4 "todo" /usr/src/linux-headers-4.13.0-25/include/soc/tegra/bpmp-abi.h
grep -m2 -P --color=never "huge\thuge\tADJ\t" ~/Research/Data/UD_English-EWT/en-ud-dev.conllu
grep -m2 -P -C5 --color=never "huge\thuge\tADJ\t" ~/Research/Data/UD_English-EWT/en-ud-dev.conllu
sed
and awk
¶sed
and awk
are both extremely popular tools for text manipulation from the command linesed
is short for Stream EDitorawk
is named after the last names of the 3 creatorssed
and awk
are full-fledges scripting languges in their own rightawk
Scripts¶awk
script is a series of rules of the formatpattern { action }
awk
script is running either directly as an argument to the awk
command, or from a file specified to awk
awk 'SCRIPT' INPUT_FILE
awk -f SCRIPT_FILE INPUT_FILE
awk
Scripts Continued¶awk
{ print $0; }
awk '/^import/ { print $0; }' /usr/local/lib/python3.6/dist-packages/jupyter_core/*.py
awk '/^import/' /usr/local/lib/python3.6/dist-packages/jupyter_core/*.py
awk '{ print $0; }' /usr/local/lib/python3.6/dist-packages/jupyter_core/*.py
awk
Variables¶awk
was designed for space seperated data files-F
flag$0
$1
, $2
, and so onawk
Variables¶awk
has many built in variables that contain useful informationawk 'NF > 12 { print $0; }' /usr/local/lib/python3.6/dist-packages/jupyter_core/*.py
awk 'length($0) > 80 { print $0; }' /usr/local/lib/python3.6/dist-packages/jupyter_core/*.py
awk 'END { print NR; }' /usr/local/lib/python3.6/dist-packages/jupyter_core/*.py
awk '/^import/ {print $2;}' /usr/local/lib/python3.6/dist-packages/jupyter_core/*.py
awk '/^import/ {print $2;}' /usr/local/lib/python3.6/dist-packages/jupyter_core/*.py | sort -u
awk -F/ '/^#!/ {print $(NF);}' src/**/*
sed
¶sed
reads in a file, applies a command to each line in a file, and prints the result to STDOUT-i
flag. Be careful with this!-n
flagawk
, sed
allows us to control which lines we apply the commands topaste <(head data/words.txt) <(tail data/words.txt)
wc -l data/words.txt
sed -n '21p' data/words.txt
sed -n '21,25p' data/words.txt
sed -n '/[A-Z][a-z]/p' data/words.txt
sed -n '/[[:digit:]][[:digit:]]/p' data/words.txt
sed -n '1,170!p' data/words.txt
sed -n '/^[a-zA-Z]/!p' data/words.txt
sed
Commands¶sed '/^\s*#[^!]/d' src/shell/read_ps_example.sh
sed '5,$d' src/shell/read_ps_example.sh
sed '/^\s*#!/a # There is a shebang line in this file' src/shell/read_ps_example.sh
sed '$a #This is the end of the file' src/shell/read_ps_example.sh
sed '$i \\n#Only One Line Left' src/shell/read_ps_example.sh
sed 's/u/i/' src/shell/read_ps_example.sh
sed 's/u/i/g' src/shell/read_ps_example.sh
sed '/^#[^!]/s/u/i/g' src/shell/read_ps_example.sh
ls -lh data/* | sed -r '/[[:digit:]]+[KM]\s/s/.*[^[:alpha:].]([[:alpha:].]+)$/\1/g'
print X
should be replaced to print(X)
bash
behave like functions in most other languages, with a few notable differencesbash
command rather than a functionFUNCTION_NAME()
{
#CODE HERE
}
FUNCTION_NAME ARG1 ARG2 ARG3
hello(){
echo "Hello World";
}
hello
today() {
date +"%A, %B %-d, %Y"
}
echo -n "Today is "
today
ls(){
command ls -lh
}
#ls
$1
, $2
, etc refer to the arguments passed to the function$#
holds the number of arguments passedhello(){
echo "Hello $1!"
}
hello World
hello Class
hello
hello The Whole World
print_all_args(){
echo $@
echo $#
}
print_all_args How Many Args Are Printed?
count_files_of_type() {
echo $(/bin/ls -1 *.$1 | wc -l)
}
count_files_of_type sh
count_files_of_type ipynb
count_files_of_type pl
count_files_of_type
do_math()
{
a=$1
echo $((a + 1))
}
do_math 10
bash
does support the return
keyword, but this can only return integer values$?
variable$(FUNCTION_NAME ARG1 ARG2)
sum(){
sum=0
for num in $@; do
sum=$((sum + num))
done
return $sum
}
sum 1 2 3 4 5
echo "The sum is $?"
sum(){
sum=0
for num in $@; do
sum=$((sum + num))
done
echo $sum
}
total=$(sum 1 2 3 4 5)
echo $((total + 1))
echo "The sum is $total"
count_files_of_type2() {
/bin/ls -1 *.$1 | wc -l
}
echo "There are $(count_files_of_type2 sh) .sh files"
bash
Functions¶bash
doesn't have a default concept of variable scope except for the special variables $1
, $2
, etc.local
before the assignment operator will ensure that the scope of the variable is only the function itselfx=1
mess_up_the_scope(){
x=2
}
echo "The value of x is $x"
mess_up_the_scope
echo "The value of x is $x"
x=1
cant_mess_up_the_scope(){
local x=2
}
echo "The value of x is $x"
cant_mess_up_the_scope
echo "The value of x is $x"