1, vi-like regular instructions
0: move to the beginning of the line
$: move to the end of the line
G: move to the last line of the file
nG: move to the nth line of the file
dd: delete the line where the cursor is located
ndd: delete n lines of text including the cursor
u: restore the text that was just modified
Find the contents of a file
/word means to search the file for the string "word" from top to bottom
?word means to search the file for the string "word" from the bottom up
file modification
Chestnut:
Change old between 50 and 100 lines to new, and select whether they need to be modified one by one
: 50, 100s/old/new/gc plus the c command will prompt the user for confirmation for each replacement action
Copy the ten lines from 51 to 60 and paste it after the last line
51,60 co $
Select all blank lines and delete them
:g/^\s*$/d
:g means in full document scope
^ represents the start of a line
\s* represents whitespace characters
$ represents end of line
d means delete
Second, the use of grep command and regular expressions
-c count the number of lines in the file or text containing the matching string grep -c "file" file.txt
-n print the number of lines containing the matched string grep -n "file" file.txt
Some Useful Cases of grep and Regular Expressions
matches lines containing the string "408"
grep -n "\<408\>" file.txt
Explanation : \<408\> Because it is a string, add \< and \> to determine the anchoring of the beginning and end of the word.
Find lines with code 1001 or 1002
grep -n 100[12] file.txt
Display lines whose line start is not 4 or 5
grep -n ^[^45] file.txt
Find all lines that contain a string starting with K and ending with D
grep -n '\<K[[:alnum:]]*D\> ' file.txt
grep -n '\<K[a-zA-Z0-9]*D\>' file.txt
Explanation: [[:alnum:]] means matching alphanumeric characters. The * is added after, because there are multiple matches. * means match the preceding character any number of times.