1/26
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
What does VIM stand for?
VIsual editor iMproved
Why is VIM so popular?
it's very lightweight, free, and has many shortcuts for ease of use (who needs a mouse!)
What are the 3 modes of VIM?
Command/Window, Insert, and Line Command
Command/Window Mode VIM
where we can execute commands
- basic cursor motions, screen control, word commands, deletions, etc.
Character Insert Mode VIM
where text can be entered
- use many different letters to enter: i - insert before; o - on the next line, a - insert after, O - previous line
Line Command Mode VIM
where ex or ed commands can be issued
gvim
VIM with a GUI; need X11 forwarding to work on agora
Enhanced for
for
commands
done
- can use break or continue in a loop as well; break causes you to leave the loop, continue goes to next iteration
Arrays
declared like x=("lemur" "dog" "cat")
- read x[7] x[9] to store elements in array
- to display echo ${x[0]} -> lemur
- echo${#x[@]} to see amount of elements in array
Functions
declared like:
list()
{
first=$1
mid=$2
last=$3
strange="What is my scope" // global
local foo="huh" // local to function
}
- $1/2/3 are all parameters
awk
awk -[Fc] 'script' [files] ...
awk -[Fc] -f scriptfile [files]...
-Fc - c is a character to act as a field separator; default of space and tab
-f - defines a script file to use
uses pattern {procedure}; if no pattern is given procedure applied to everyline. if no procedure is given, outputs matched pattern to stdout
awk patterns
- /regular expressions/
- /D[Rr]\./ -> matches Dr. or DR. on any line
- relational expressions
- $0 is entire line
- $n means nth field
- NF is number of fields in current line
- NR is the number of the current line
- pattern matching expression
- $1 == 'if' is if the first field is 'if'
- $1 == $2 is if the first field matches the second
- NR > 100 have I processed 100 lines?
- /if/ && /
awk procedures
- specifies the processing of a line that matches a given pattern
- contained with { } and consists of commands separated by semicolons or newlines
ex:
- { print $1, $2 } # prints first 2 fields of each line
- BEGIN {sum=0}
{sum += 1} # sums values of fields in first column
END{print sum} # and prints the sum
tr
character translator filter; common use to replace one character for another
- reads stdin and writes stdout
forms:
tr str1 str2 # replace str1 with corresponding char in str2
tr -d string # delete all occurrences of characters in the string
tr -s str1 str2 # replace instances of repeated chars in str1 w/
# single char in str2
ex:
- tr "[a-z]" "[A-Z]" # lower -> upper
- tr -s "\t" "\t" # squeeze consecutive tabs to a single tab
- tr -d '\015' # delete carriage returns
sort
sort or merge lines of text or records
- default sorts be characters
-n - sort by numerics
-t - change field separator (-t: changes to :)
-b - ignores blank space
-r - reverse order
-kn - sorts by the nth field
sort -k3,k3 -k1 file.txt - sorts by 3rd field then stops and goes to 1st field
fmt
puts specified amount of characters line by line
ex: fmt -#
-# - number of characters per line; default of 65
head
looks at the beginning lines of a file
- head
-# - number of lines to show
tail
shows the last lines of a file
- tail
-# - number of lines to show
-f - monitors the end of a file; updates as you add to the end of a file (WITH RETRY)
-F - if iNode detaches it'll go and find it for the new one; so it updates on edits (WITHOUT RETRY)
basename
when given a path to a file, returns the name of the file
basename
-a - basename of all *.extension files
-s .ext - removes the file extensions
dirname
when given a path to a file, returns the path to the file
cut
allows you to select fields from files
-d - set field separator/delimeter for file
-f# - number of the field to
cut -d " " -f1 -> gets first field from file
cut -d " " -f2,4 -> fields 2 and 4
cut -d " " -f2-4 -> fields 2 through 4
paste
merges corresponding or subsequent lines of files
-dx -> sets x as the delimeter between values in output
paste -d " " file1.txt file2.txt -> merges two files together and print to stdout
sed
stream editor
- sed "script" [file]...
- sed -e "script" [ -e "script" ]... [file]....
- applies n scripts to n files
- sed -f scriptFile [file]...
flags:
-e - specifies to use a script (also to use multiple scripts)
-f - specifies to use a script file
-E - regex extended (POSIX compliant)
ex: sed "50d" file.txt -> delete file 50 from file.txt
Line address Commands in sed
n - absolute line number n
i,j - range from lines i to j
- can also use . for current line and $ for last line
+ - go forwards one line
- - go backwards one line
+n - go forwards n lines
-n - go backwards n lines
ex: 1,$ is the entire file
Line editing commands
addr - go to address
[addr] 1 - print lines to stdout
/
?
[addr] a text - add text to line after address
[addr] i text - add text to line before address
[addr] d - delete line at address
[addr] c text - replace text at address
[addr] s pat1/pat2 - replace pattern 1 with pattern 2 at address
& - what you match
/g - every occurrence on a single line
ex:
/^#/d - delete comments that are at the start of the line
/Smith/,$d - delete from first line with Smith to the end
sed examples
s/[A-Z][A-Z][A-Z]*/"&"/g
- quote every instance of 2 or more characters in a file
s/[A-Z][A-Z][A-Z]*/"&"/2
- same as above, only the second occurrence per line
applying multiple commands in sed
can use { } to practically define a function
ex:
/^
s/^
s/^
s/^<\/ul>/<\/ol>/
} -> replace all of the following inside of