-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathpattern-matching.txt
More file actions
54 lines (46 loc) · 1.78 KB
/
pattern-matching.txt
File metadata and controls
54 lines (46 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Pattern Matching
GREP
grep -r pattern dir
--grep recursively for pattern in any file nested in dir
egrep '(this|that)' file
OR
grep -e '(this|that)' file
-grep against target file basic regex accepting patterns using basic regex
egrep '[^\ ]\{8,\}' target
-pattern-match all 8+ digit chars
SED
sed 's/[[:space:]]//g'
-trim whitespaces from a file
AWK
awk 'NR==FNR{a[$0];next}!($0 in a)' file2 file1
--Compare 2 files and return the unique lines in first file
awk NF -
--remove newlines from stdin
awk 'match($0,"="){print substr($0,RSTART-5,5)}'
-print characters before+after a pattern is matched
awk -F":" '{print $2}' file.txt
awk -F":" '{if ($2)print $2}' file.txt
-print second colum of ":"-delimited lines in a file
awk -F, '{ print $1 "-" $3 "-" $5 }' file.csv
-print alternating lines of a csv file
awk -F: '{ print $NF }' file.txt
-print last column of delimited text file
awk -F":" '/regex_here/{print $2}' file.txt
-print second field of lines matching regex from delimited file
awk '/exp/,/end/' file.txt
-print all lines in a file from match "exp" to match "end"
awk '!/regex/{print $1 " " $2 }' file.txt
-print all lines not matching regex from first two whitespace-delimited fields
awk '/regex/&&!/regex2/{print $1}' file.txt
-print all lines matching regex while not matching regex2
awk '{print length, $0}' file.txt | sort -n | cut -d " " -f2-
-sort wordlists by length
xclip -selection clipboard -o | sed 's/[[:space:]]//g' | awk NF - >http_response_codes.txt
--remove all whitespace and newlines from a copied selection of text (from clipboard)
Bash variables
${var% *}
--match all characters preceding whitespace
${var##/*}
--match all characters following "/"
if [[ "${var}" != "${var% *}" ]]; then
--conditional operation testing if string variable contains whitespace