notes update

This commit is contained in:
Hugh Ratsch
2025-03-10 18:26:54 -05:00
parent 5830b72de9
commit bbfc9df6d1
+159
View File
@@ -1207,3 +1207,162 @@ ls -l > output.txt
### redirect the output of the ls command to a file and append to the file ### redirect the output of the ls command to a file and append to the file
ls -l >> output.txt ls -l >> output.txt
``` ```
### Appending Output and Standard Error
```bash
### 1> redirects the output to a file
### 2> redirects the error to a file
### redirect the output of the ls command to a file and append to the file
ls -l >> output.txt 2>&1
### redirect the output of the ls command to a file and append to the file
ls -l >> output.txt 2>&1
```
### Understanding the Pipe
```bash
### pipe the output of the ls command to the grep command
ls -l | grep "file.txt"
### pipe the output of the ls command to the grep command and display the line number
ls -l | grep -n "file.txt"
cat file.txt | wc -l # count the number of lines in the file
```
### Filtering with Grep
```bash
### grep the word "file" in the file.txt file
grep "file" file.txt
### grep the word "file" in the file.txt file and display the line number
grep -n "file" file.txt
```
### Digging into Regular Expressions
```bash
### ^ matches the beginning of a line
### $ matches the end of a line
### . matches any single character
### * matches zero or more of the preceding character
### + matches one or more of the preceding character
### ? matches zero or one of the preceding character
### [a-z] matches any single character in the range a-z
### [^a-z] matches any single character not in the range a-z
### grep with regular expressions
ls -a | grep ".py$" # grep the files that end with .py
ls -a | grep "^.py" # grep the files that start with .py
grep "^ch" file.txt # grep the lines that start with ch
grep "^c.t$" file.txt # grep the lines that start with c and end with t but only if the line is exactly c.t
grep "^c*t$" file.txt # grep the lines that start with c and end with t and have any number of characters in between
grep "^[bc].*t$" file.txt # grep the lines that start with b or c and have any number of characters in between and end with t
```
### Head and Tail
```bash
### head
head -n 5 file.txt # display the first 5 lines of the file
### tail
tail -n 5 file.txt # display the last 5 lines of the file
tail -f file.txt # display the last 5 lines of the file and follow the file
```
### Creating Hard Links and Soft Links
```bash
### hard link is a physical link to the file
ln file.txt file_link.txt # create a hard link to the file
### soft link is a symbolic link to the file
ln -s file.txt file_link.txt # create a soft link to the file
```
## Create & Transform Text Files
### Text Editing with Nano
```bash
nano file.txt # edit the file
### save and exit
ctrl + x
ctrl + o # save the file
```
### Understanding Vim Basics
```bash
vim file.txt # edit the file
### open vim with line number
vim +5 file.txt # open the file and go to line 5
### exit without saving
:q!
### save and exit
:wq
### insert mode
i # insert mode
### command mode
: # command mode
### search
/ # search
### search and replace
:%s/old/new/g # replace all instances of old with new
:%! sort # sort the file
### navigation
h # move left
j # move down
k # move up
l # move right
2h # move 2 characters left
37j # move 37 lines down
```
### Digging Deeper with Vim
```bash
### vim with line number
vim +5 file.txt # open the file and go to line 5
### vim with line number and column number
vim +5:10 file.txt # open the file and go to line 5 and column 10
dd # delete the current line
5dd # delete the next 5 lines
yy # copy the current line
p # paste the copied line
u # undo the last action
d2j # delete the current line and move the cursor to the next line
dip # quickly delete the current paragraph
yip # quickly copy the current paragraph
```
### Manipulating Files with Awk
```bash
awk '{print $1}' file.txt # print the first field
awk '{print $1, $2}' file.txt # print the first and second fields
awk '{print "Hello, " $1}' file.txt # print "Hello, " and the first field
awk -F: '{print $NF}' file.txt # print the last field separated by a colon
```
### Manipulating Files with Sed
```bash
sed 's/old/new/' file.txt # replace old with new
sed 's/old/new/g' file.txt # replace all instances of old with new
```