Added SED and AWK Sheets.

This commit is contained in:
CSnap 2023-11-19 09:51:02 +00:00
parent 14dd29920f
commit 6109ca4e51
6 changed files with 1072 additions and 272 deletions

272
LEMP.md
View File

@ -1,272 +0,0 @@
# Fedora Super-LEMP setup:
*Based on https://www.howtoforge.com/how-to-install-nginx-with-php-and-mariadb-lemp-stack-on-fedora-32/*
## Install packages
### Massive swiss-army knife setup
```
dnf install certbot certbot-nginx cockpit htop iftop iptraf nano openssh-server net-tools nginx* rsync screen vim wget && dnf groupinstall "Development Tools" "Web Server" "Mysql" "php"
```
### Or Less Extra
`dnf install certbot certbot-nginx nginx `
`dnf install vim nano rsync screen vim wget net-tools htop iftop iptraf openssh-server bash-completion`
`dnf groupinstall "Development Tools" "Web Server" "Mysql" "php"`
### More butter Rocky variant
`dnf install epel-release`
`dnf install git vim nano rsync screen vim wget net-tools htop iftop iptraf openssh-server bash-completion mariadb mariadb-server certbot python3-certbot-nginx nginx php-fpm`
`dnf groupinstall "Development Tools"`
## Add non-root administrator
`adduser user`
`usermod -aG wheel user`
`passwd user`
`vi /etc/sudoers`
`sudo -i -u user`
## Configure SSH
`ssh-keygen -t rsa -b 4096`
### Change port and root login settings
`vi /etc/ssh/sshd_config`
### Add keys ( also see `ssh-copy-id` )
`vi .ssh/authorized_keys`
## Firewall settings
```
systemctl enable firewalld
systemctl start firewalld
systemctl stop firewalld
systemctl restart firewalld
firewall-cmd --state
firewall-cmd --set-default-zone=public
firewall-cmd --zone=public --permanent --list-services
firewall-cmd --zone=public --permanent --add-service=http
firewall-cmd --zone=public --permanent --add-service=https
firewall-cmd --add-port 20022/tcp
firewall-cmd --permanent --add-port 20022/tcp
firewall-cmd --permanent --add-port YOUR_PORT_HERE/tcp
firewall-cmd --remove-service ssh --permanent
firewall-cmd --reload
systemctl reload firewalld
```
## MariaDB
```
systemctl enable mariadb
systemctl start mariadb
mysql_secure_installation # Y-N-Y-Y-Y-Y
mysql -u root -p
CREATE USER 'user1'@localhost IDENTIFIED BY 'password1';
CREATE USER 'namenode'@localhost IDENTIFIED BY ':passwd';
GRANT ALL PRIVILEGES ON *.* TO 'user1'@localhost IDENTIFIED BY 'password1';
GRANT ALL PRIVILEGES ON *.* TO 'user2'@localhost IDENTIFIED BY 'passwd2';
FLUSH PRIVILEGES;
SHOW GRANTS FOR 'user1'@localhost;
SHOW GRANTS FOR 'user2'@localhost;
CREATE DATABASE 'yourDB';
SHOW DATABASES;
DROP USER 'user1'@localhost; # Just for example to show how to delete a user
```
## Redis Setup
`dnf install redis php-redis`
`sudo systemctl enable --now redis`
`vi /etc/redis/redis.conf`
Change bind (0.0.0.0), `requirepass`, `port (2*)`, `maxmemory` (256mb), and `maxmemory-policy allkeys-lru`.
`systemctl restart redis`
```
firewall-cmd --zone=public --permanent --add-port=26379/tcp
firewall-cmd --reload
```
## NGINX
### Important working directories:
```
/usr/share/nginx/
/etc/nginx/
```
### Create user working directory for custom configuration files:
```
mkdir /etc/nginx/sites-available # Create a directory for nginx.conf files
mkdir /usr/share/nginx/example.com/html -p # Create new webroot with specified structure
```
### Now we can create a new config file to start with:
`vi /etc/nginx/sites-available/example.com.conf`
### Link it to active conf directory
`ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/conf.d/`
### If it is required at some point, removing that symlink is as easy as:
`rm /etc/nginx/conf.d/example.com.conf`
### Now we edit the nginx.conf
`vi /etc/nginx/nginx.conf`
### Set the following lines after the line "include /etc/nginx/conf.d/*.conf" (if not already set):
```
server_names_hash_bucket_size 64; # Should already exist in recent versions
types_hash_max_size 4096; ## Should already be set
```
### Comment out the root location directive (Can uncomment after setup so as not to confuse cache while testing?)
**To test and reload the configuration:**
`nginx -t`
`systemctl reload nginx`
### Simple recap moving forward:
```
systemctl start nginx
systemctl restart nginx
systemctl enable nginx
systemctl status nginx
systemctl reload nginx
nginx -t
mkdir /etc/nginx/sites-available
mkdir /usr/share/nginx/example.com/html -p
vi /etc/nginx/sites-available/example.com.conf
ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/conf.d/
vi /etc/nginx/nginx.com # comment out the root in default server block (troubleshooting)
systemctl reload nginx
```
## PHP-FPM setup
### Change user in configuration (nginx):
`vi /etc/php-fpm.d/www.conf`
`systemctl enable php-fpm`
`systemctl restart php-fpm`
### PHP-OPCache setup
`vi /etc/php.d/10-opcache.ini`
```
opcache.enable_cli=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
```
`systemctl restart php-fpm`
`systemctl reload nginx`
### phpMyAdmin setup
`dnf install phpmyadmin `
`ln -s /usr/share/phpMyAdmin/ /usr/share/nginx/hosting.namenode.xyz/dbpma`
`chown -R nginx:nginx /var/lib/php/session`
`chown -R nginx:nginx /var/lib/phpMyAdmin`
`chown -R nginx:nginx /etc/phpMyAdmin`
`vi /etc/phpMyAdmin/config.inc.php`
```
$cfg['Servers'][$i]['AllowNoPassword'] = false;
$cfg['Servers'][$i]['AllowRoot'] = false;
$cfg['TempDir'] = '/var/lib/phpMyAdmin/temp';
```
`systemctl reload php-fpm`
`systemctl reload nginx`
### Securing phpMyAdmin further
```
vi pass-infile ## make a password for openssl to encrypt - one line no spaces
```
```
openssl passwd -in pass-infile ## Copy the output (your encrypted password)
```
```
vi /etc/nginx/pma_pass # Create a user/pass pair for the authentication gateway.
```
### Format:
```
user:p@s$w0Rd # one line
```
### Add the required "dbpma" section
`vi /etc/nginx/sites-available/example.com.conf`
`systemctl reload nginx`
[Install and secure PMA with NGINX Ubuntu 18.04](https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-with-nginx-on-an-ubuntu-18-04-server)
## Cockpit Setup
`vi /etc/cockpit/cockpit.conf`
`vi /etc/nginx/sites-available/example.com.conf`
[Proxying Cockpit over NGINX](https://github.com/cockpit-project/cockpit/wiki/Proxying-Cockpit-over-nginx)
[Reverse proxy Cockpit over NGINX](https://www.freesoftwareservers.com/display/FREES/Reverse+Proxy+Cockpit+over+NGinX)
## Certbot setup (Examples)
```
certbot --nginx -d example.com -d www.example.com
certbot --nginx --agree-tos -d example.com -d www.example.com --email your-email-address
certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --must-staple -d example.com -d www.example.com --email your-email-address
```
`$ EDITOR=vim crontab -e`
```
25 2 * * 0 /usr/bin/certbot renew --quiet # Every Sunday 2:25am
```

View File

@ -3,6 +3,8 @@
#### ***A compendium of various technical information.*** #### ***A compendium of various technical information.***
## Cheat Sheets ## Cheat Sheets
- [AWK]((https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/awk.md)
- [AWKWARD- AWK and SED examples](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/awkward.md))
- [BASH - The Bourne Again Shell](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/BASH.md) - [BASH - The Bourne Again Shell](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/BASH.md)
- [CSS - Cascading Style Sheets](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/CSS.md) - [CSS - Cascading Style Sheets](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/CSS.md)
- [HTML - The HyperText Markup Language](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/HTML.md) - [HTML - The HyperText Markup Language](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/HTML.md)
@ -12,6 +14,7 @@
- [Perl - Practical Extraction and Reporting Language](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/perl.md) - [Perl - Practical Extraction and Reporting Language](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/perl.md)
- [PHP - Proper Hypertext Pre-processor](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/PHP.md) - [PHP - Proper Hypertext Pre-processor](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/PHP.md)
- [Python - High-level, general-purpose programming language](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/python.md) - [Python - High-level, general-purpose programming language](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/python.md)
- [SED - The Stream Editor](- [AWK]((https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/sed.md))
- [SQL - Structured Query Language](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/SQL.md) - [SQL - Structured Query Language](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/SQL.md)
## Guides ## Guides

158
awk.md Normal file
View File

@ -0,0 +1,158 @@
# AWK Cheat Sheet
## Table of Contents
1. [AWK Basics](#awk-basics)
2. [Patterns and Actions](#patterns-and-actions)
3. [Variables](#variables)
4. [Operators](#operators)
5. [Built-in Variables](#built-in-variables)
6. [Control Structures](#control-structures)
7. [Functions](#functions)
8. [Text Processing](#text-processing)
9. [Regular Expressions](#regular-expressions)
10. [Arrays](#arrays)
11. [User-Defined Functions](#user-defined-functions)
12. [Command Execution](#command-execution)
## AWK Basics
### Print Lines
```awk
awk '{print}' filename
```
### Print Specific Column
```awk
awk '{print $2}' filename
```
### Filter with Condition
```awk
awk '$1 > 10 {print}' filename
```
## Patterns and Actions
```awk
# Pattern with Action
pattern1 { action1 }
pattern2 { action2 }
```
## Variables
```awk
# Assigning Variables
variable = value
# Accessing Variables
print variable
```
## Operators
```awk
# Arithmetic Operators
+, -, *, /, %
# Relational Operators
==, !=, >, <, >=, <=
# Logical Operators
&&, ||
```
## Built-in Variables
```awk
# NR: Record Number
NR > 1 {print "Line Number: " NR}
# NF: Number of Fields
{print "Number of Fields: " NF}
```
## Control Structures
```awk
# if Statement
if (condition) {
# Commands
}
# if-else Statement
if (condition) {
# Commands for true condition
} else {
# Commands for false condition
}
```
## Functions
```awk
# Built-in Functions
{
result = sqrt(9)
print "Square Root: " result
}
```
## Text Processing
### Field Separator
```awk
# Change Field Separator
awk -F',' '{print $2}' filename
```
### Output Formatting
```awk
# Formatting Output
awk '{printf "Name: %-10s Age: %d\n", $1, $2}' filename
```
## Regular Expressions
```awk
# Match Pattern
awk '/pattern/ {print}' filename
```
## Arrays
```awk
# Declare an Array
array[1] = "apple"
array[2] = "banana"
# Access Array Element
print array[1]
```
## User-Defined Functions
```awk
# Define a Function
function my_function(parameter) {
# Commands
}
# Call the Function
my_function(value)
```
## Command Execution
```awk
# Execute Command
awk '{cmd = "echo " $1; system(cmd)}' filename
```

700
awkward.md Normal file
View File

@ -0,0 +1,700 @@
# AWK and SED Examples
These examples explore the combined use of AWK and SED for text processing tasks.
## Example 1: AWK and SED for Text Transformation
### Input File (input.txt)
```
John Doe,25,Engineer
Jane Smith,30,Designer
Bob Johnson,22,Developer
```
### Goal
We want to transform the CSV data into a formatted table with headers.
### Solution
```bash
awk -F',' '{printf "%-15s %-3s %-10s\n", $1, $2, $3}' input.txt | sed '1i\
Name Age Job\
--------------- --- ----------'
```
### Output
```
Name Age Job
--------------- --- ----------
John Doe 25 Engineer
Jane Smith 30 Designer
Bob Johnson 22 Developer
```
## Example 2: AWK and SED for Selective Printing
### Input File (input.txt)
```
apple
banana
cherry
date
```
### Goal
Print lines containing "a" or "e" using AWK and SED.
### Solution
```bash
awk '/a|e/' input.txt | sed -n '/a\|e/p'
```
### Output
```
apple
banana
date
```
## Example 3: AWK and SED for Filtering and Sorting
### Input File (input.txt)
```
apple,5
banana,2
cherry,8
date,1
```
### Goal
Filter lines with the second column greater than 2, sort by the second column, and add a header.
### Solution
```bash
awk -F',' '$2 > 2' input.txt | sort -t',' -k2n | sed '1i\
Fruit,Quantity'
```
### Output
```
Fruit,Quantity
apple,5
cherry,8
```
## Example 4: AWK and SED for Text Extraction
### Input File (input.txt)
```
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.
```
### Goal
Extract sentences containing the word "sed" and replace "sed" with "AWK".
### Solution
```bash
awk 'BEGIN{RS="\\."} /sed/' input.txt | sed 's/sed/AWK/g'
```
### Output
```
Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua
```
## Example 5: AWK and SED for HTML Tag Extraction
### Input File (input.html)
```html
<div class="container">
<p>This is a paragraph.</p>
<a href="https://example.com">Visit our website</a>
</div>
```
### Goal
Extract and list all HTML tags present in the file.
### Solution
```bash
sed -n 's/<[^>]*>//p' input.html | awk '{print tolower($0)}'
```
### Output
```
div class="container"
p
a href="https://example.com"
/a
```
## Example 6: AWK and SED for JSON Formatting
### Input File (input.json)
```json
{
"name": "John Doe",
"age": 30,
"city": "New York"
}
```
### Goal
Format the JSON to be human-readable using AWK and SED.
### Solution
```bash
awk -v RS= '{$1=$1}1' input.json | sed 's/":"/": "/g; s/{/{\n/g; s/}/\n}/g'
```
### Output
```
{
"name": "John Doe",
"age": 30,
"city": "New York"
}
```
## Example 7: AWK and SED for Log Analysis
### Input File (logfile.txt)
```
2023-01-15 10:23:15 INFO User login successful
2023-01-15 10:30:42 ERROR Invalid password attempt
2023-01-15 11:05:18 INFO User logout
```
### Goal
Extract and format log entries containing "ERROR".
### Solution
```bash
awk '/ERROR/' logfile.txt | sed 's/\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\} [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}\) \(ERROR .*\)/Timestamp: \1\n\2\n/'
```
### Output
```
Timestamp: 2023-01-15 10:30:42
ERROR Invalid password attempt
```
## Example 8: AWK and SED for CSV Transformation
### Input File (data.csv)
```
Name,Age,Occupation
John,28,Engineer
Jane,35,Designer
Bob,22,Developer
```
### Goal
Transform the CSV data into a tab-separated format.
### Solution
```bash
awk -F',' '{printf "%s\t%s\t%s\n", $1, $2, $3}' data.csv | sed '1i\
Name\tAge\tOccupation'
```
### Output
```
Name Age Occupation
John 28 Engineer
Jane 35 Designer
Bob 22 Developer
```
## Example 9: AWK and SED for XML Tag Removal
### Input File (data.xml)
```xml
<root>
<item>Apple</item>
<item>Banana</item>
<item>Cherry</item>
</root>
```
### Goal
Remove all XML tags and extract the content.
### Solution
```bash
sed 's/<[^>]*>//g' data.xml | awk 'NF'
```
### Output
```
Apple
Banana
Cherry
```
## Example 10: AWK and SED for Markdown Table Conversion
### Input File (table.md)
```markdown
| Name | Age | Occupation |
|-------|-----|-------------|
| John | 28 | Engineer |
| Jane | 35 | Designer |
| Bob | 22 | Developer |
```
### Goal
Convert the Markdown table into a CSV format.
### Solution
```bash
sed -n '/|/p' table.md | awk -F'|' '{gsub(/^[ \t]+|[ \t]+$/, "", $2); gsub(/^[ \t]+|[ \t]+$/, "", $3); gsub(/^[ \t]+|[ \t]+$/, "", $4); printf "%s,%s,%s\n", $2, $3, $4}'
```
### Output
```
Name,Age,Occupation
John,28,Engineer
Jane,35,Designer
Bob,22,Developer
```
## Example 11: AWK and SED for HTML List Extraction
### Input File (list.html)
```html
<ul>
<li>Apple</li>
<li>Banana</li>
<li>Cherry</li>
</ul>
```
### Goal
Extract and list items from an HTML unordered list.
### Solution
```bash
sed -n '/<li>/s/<li>\(.*\)<\/li>/\1/p' list.html | awk '{print "- " $0}'
```
### Output
```
- Apple
- Banana
- Cherry
```
## Example 12: AWK and SED for YAML to JSON Conversion
### Input File (data.yaml)
```yaml
name: John Doe
age: 30
city: New York
```
### Goal
Convert YAML data into JSON format.
### Solution
```bash
sed -n '/^[^#]/s/: /: "/p' data.yaml | awk '{printf "%s", $0} END {print "\""}' | sed 's/"/\\"/g' | awk 'BEGIN {print "{"} {print " " $0} END {print "}"}'
```
### Output
```
{
name: "John Doe",
age: "30",
city: "New York"
}
```
## Example 13: AWK and SED for Extracting URLs
### Input File (text_with_urls.txt)
```
Visit our website: https://example.com
For more information, check out: https://documentation.com
```
### Goal
Extract and list all URLs from the text.
### Solution
```bash
sed -n 's/.*\(https\?:\/\/[^ ]*\).*/\1/p' text_with_urls.txt | awk '!a[$0]++'
```
### Output
```
https://example.com
https://documentation.com
```
## Example 14: AWK and SED for Data Filtering and Sorting
### Input File (data.txt)
```
Name|Age|Occupation
John|28|Engineer
Jane|35|Designer
Bob|22|Developer
```
### Goal
Filter entries with Age greater than 25 and sort by Age.
### Solution
```bash
awk -F'|' '$2 > 25' data.txt | sort -t'|' -k2n | sed '1i\
Name|Age|Occupation'
```
### Output
```
Name|Age|Occupation
Jane|35|Designer
John|28|Engineer
```
## Example 15: AWK and SED for Line Reversal
### Input File (reverse_me.txt)
```
Line 1
Line 2
Line 3
```
### Goal
Reverse the order of lines in the file.
### Solution
```bash
awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' reverse_me.txt | sed '1i\
Reversed Lines'
```
### Output
```
Reversed Lines
Line 3
Line 2
Line 1
```
## Example 16: AWK and SED for Word Frequency Analysis
### Input File (text_analysis.txt)
```
apple orange banana
banana apple orange
orange banana apple
```
### Goal
Count the frequency of each word and sort the results.
### Solution
```bash
awk '{for(i=1; i<=NF; i++) words[$i]++} END {for(word in words) print word, words[word]}' text_analysis.txt | sort -k2n -r | sed '1i\
Word Frequency'
```
### Output
```
Word Frequency
banana 3
apple 3
orange 3
```
## Example 17: AWK and SED for JSON Pretty Printing
### Input File (json_data.json)
```json
{"name":"John","age":30,"city":"New York"}
```
### Goal
Format the JSON data for better readability.
### Solution
```bash
awk -v RS= '{$1=$1}1' json_data.json | sed 's/":"/": "/g; s/{/{\n/g; s/}/\n}/g'
```
### Output
```
{
"name": "John",
"age": 30,
"city": "New York"
}
```
## Example 18: AWK and SED for Extracting Email Addresses
### Input File (emails.txt)
```
Contact us at: info@example.com
For support, email support@example.com
```
### Goal
Extract and list all email addresses from the text.
### Solution
```bash
sed -n 's/.*\b\([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}\)\b.*/\1/p' emails.txt | awk '!a[$0]++' | sed '1i\
Email Addresses'
```
### Output
```
Email Addresses
info@example.com
support@example.com
```
## Example 19: Convert Markdown Headings to HTML
### Input (markdown_headings.md)
```markdown
# Heading 1
## Heading 2
### Heading 3
```
### Goal
Convert Markdown headings to HTML headings.
### Solution
```bash
sed -n 's/^# \(.*\)$/\<h1\>\1\<\/h1\>/p; s/^## \(.*\)$/\<h2\>\1\<\/h2\>/p; s/^### \(.*\)$/\<h3\>\1\<\/h3\>/p' markdown_headings.md
```
### Output
```html
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
```
## Example 20: Convert Markdown Lists to HTML
### Input (markdown_lists.md)
```markdown
- Item 1
- Item 2
- Subitem A
- Subitem B
```
### Goal
Convert Markdown lists to HTML lists.
### Solution
```bash
awk '/^-/ {gsub(/^-/, "<li>"); print "<ul>"; print $0} !/^-/ {gsub(/^ /, ""); print $0 "</li>"} END {print "</ul>"}' markdown_lists.md
```
### Output
```html
<ul>
<li>Item 1</li>
<li>Item 2<ul><li>Subitem A</li><li>Subitem B</li></ul></li>
</ul>
```
## Example 21: Convert Bold and Italic Markdown to HTML
### Input (markdown_bold_italics.md)
```markdown
**Bold Text** *Italic Text*
```
### Goal
Convert Markdown bold and italic text to HTML.
### Solution
```bash
sed -e 's/\*\*\(.*\)\*\*/\<strong\>\1\<\/strong\>/g' -e 's/\*\([^*]*\)\*/\<em\>\1\<\/em\>/g' markdown_bold_italics.md
```
### Output
```html
<strong>Bold Text</strong> <em>Italic Text</em>
```
## Example 22: Reverse the Order of Lines
### Input (original_order.txt)
```
Line 1
Line 2
Line 3
```
### Goal
Reverse the order of lines in the file.
### Solution
```bash
awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' original_order.txt
```
### Output
```
Line 3
Line 2
Line 1
```
## Example 23: Sort Lines Alphabetically
### Input (unsorted_lines.txt)
```
Banana
Apple
Cherry
```
### Goal
Sort lines alphabetically.
### Solution
```bash
sort unsorted_lines.txt
```
### Output
```
Apple
Banana
Cherry
```
## Example 24: Sort Lines by Line Length
### Input (lines_by_length.txt)
```
Short
VeryLongLineWithManyCharacters
MediumLength
```
### Goal
Sort lines by their length.
### Solution
```bash
awk '{ print length, $0 | "sort -n"}' lines_by_length.txt | cut -d ' ' -f2-
```
### Output
```
Short
MediumLength
VeryLongLineWithManyCharacters
```

79
regex.md Normal file
View File

@ -0,0 +1,79 @@
# Regular Expressions (Regex) Cheat Sheet
In this cheat sheet, we'll explore common Regular Expressions (regex) patterns and their meanings.
## Table of Contents
1. [Anchors](#anchors)
2. [Character Classes](#character-classes)
3. [Quantifiers](#quantifiers)
4. [Grouping and Capturing](#grouping-and-capturing)
5. [Alternation](#alternation)
6. [Assertions](#assertions)
7. [Modifiers](#modifiers)
8. [Escape Characters](#escape-characters)
9. [Special Characters](#special-characters)
## Anchors
- `^`: Asserts the start of a line.
- `$`: Asserts the end of a line.
- `\b`: Asserts a word boundary.
- `\B`: Asserts a non-word boundary.
## Character Classes
- `.`: Matches any single character except a newline.
- `[abc]`: Matches any one of the characters a, b, or c.
- `[^abc]`: Matches any character except a, b, or c.
- `[a-z]`: Matches any lowercase letter.
- `[A-Z]`: Matches any uppercase letter.
- `[0-9]`: Matches any digit.
- `\d`: Matches any digit (short for `[0-9]`).
- `\D`: Matches any non-digit.
- `\w`: Matches any word character (alphanumeric + underscore).
- `\W`: Matches any non-word character.
- `\s`: Matches any whitespace character.
- `\S`: Matches any non-whitespace character.
## Quantifiers
- `*`: Matches 0 or more occurrences of the preceding character or group.
- `+`: Matches 1 or more occurrences of the preceding character or group.
- `?`: Matches 0 or 1 occurrence of the preceding character or group.
- `{n}`: Matches exactly n occurrences of the preceding character or group.
- `{n,}`: Matches n or more occurrences of the preceding character or group.
- `{n,m}`: Matches between n and m occurrences of the preceding character or group.
## Grouping and Capturing
- `(...)`: Groups patterns together. Captures the matched text.
- `(?:...)`: Groups patterns together without capturing.
- `\1`, `\2`, ...: Refers to the first, second, etc. captured group.
## Alternation
- `|`: Acts like a logical OR. Matches the pattern before or after the pipe.
## Assertions
- `(?=...)`: Positive lookahead assertion.
- `(?!...)`: Negative lookahead assertion.
- `(?<=...)`: Positive lookbehind assertion.
- `(?<!...)`: Negative lookbehind assertion.
## Modifiers
- `i`: Case-insensitive matching.
- `g`: Global matching (find all matches).
- `m`: Multiline matching (`^` and `$` match the start/end of each line).
## Escape Characters
- `\`: Escapes a special character, allowing it to be treated as a literal.
## Special Characters
- `*`, `+`, `?`, `{`, `}`, `|`, `()`, `[]`, `.`, `\`: Special characters with their literal meanings when escaped.

132
sed.md Normal file
View File

@ -0,0 +1,132 @@
# Comprehensive SED Cheat Sheet
## Table of Contents
1. [SED Basics](#sed-basics)
2. [Print Operations](#print-operations)
3. [Substitution](#substitution)
4. [Deletion](#deletion)
5. [Insertion](#insertion)
6. [Append and Replace](#append-and-replace)
7. [Selective Printing](#selective-printing)
8. [Text Formatting](#text-formatting)
9. [Multiple Commands](#multiple-commands)
10. [Regular Expressions](#regular-expressions)
11. [Grouping and Backreferences](#grouping-and-backreferences)
12. [File In-Place Editing](#file-in-place-editing)
13. [SED Scripts](#sed-scripts)
## SED Basics
### Basic Syntax
```bash
sed 'command' filename
```
### In-Place Editing
```bash
sed -i 'command' filename
```
## Print Operations
```bash
# Print All Lines
sed -n 'p' filename
# Print Line Numbers
sed -n '10p' filename
# Print Range of Lines
sed -n '5,10p' filename
```
## Substitution
```bash
# Substitute Text
sed 's/pattern/replacement/' filename
# Global Substitution
sed 's/pattern/replacement/g' filename
```
## Deletion
```bash
# Delete Matching Lines
sed '/pattern/d' filename
# Delete Lines by Number
sed '5d' filename
```
## Insertion
```bash
# Insert Line Before Matching Pattern
sed '/pattern/i new_line' filename
```
## Append and Replace
```bash
# Append Line After Matching Pattern
sed '/pattern/a new_line' filename
# Replace Line with New Text
sed '/pattern/c new_line' filename
```
## Selective Printing
```bash
# Print Lines Matching Pattern
sed -n '/pattern/p' filename
```
## Text Formatting
```bash
# Convert to Uppercase
sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' filename
```
## Multiple Commands
```bash
# Execute Multiple Commands
sed -e 'command1' -e 'command2' filename
```
## Regular Expressions
```bash
# Use Regular Expressions
sed '/^pattern/s/replacement/' filename
```
## Grouping and Backreferences
```bash
# Use Grouping and Backreferences
sed 's/\(pattern1\)\(pattern2\)/\2\1/' filename
```
## File In-Place Editing
```bash
# In-Place Editing with Backup
sed -i.bak 's/pattern/replacement/' filename
```
## SED Scripts
```bash
# Create and Use SED Script
echo 's/pattern/replacement/' > myscript.sed
sed -f myscript.sed filename
```