Repository navigation
Expand file tree
/
Copy pathbash_regex.bash
More file actions
180 lines (125 loc) · 5.42 KB
/
Copy pathbash_regex.bash
File metadata and controls
180 lines (125 loc) · 5.42 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
echo hello bash
echo hello bash > stdout.txt
echo hello bash >> stdout.txt
> stdout.txt -- To empty a file content.
bad_command > stderr.txt
bad_command 2> stderr.txt
echo hello bash 1> stdout.txt
read NAME
echo $NAME
read NAME < name.txt -- Redirecting stdin to NAME variable
echo paul | read NAME
cat name.txt
cat < name.txt
echo paul | cat
touch script.sh
chmod +x script.sh
echo paul | ./script.sh
echo paul | ./script.sh 2> stderr.txt
echo paul | ./script.sh 2> stderr.txt > stdout.txt
./script.sh < name.txt 2> stderr.txt
./script.sh < name.txt 2> stderr.txt
wc kitty_ipsum_1.txt
wc -l kitty_ipsum_1.txt -- lines
wc -w kitty_ipsum_1.txt -- words
wc -m kitty_ipsum_1.txt -- characters/bytes
wc < kitty_ipsum_1.txt
echo -e "\nNumber of lines:" >> kitty_info.txt
cat kitty_ipsum_1.txt | wc -l >> kitty_info.txt
echo -e "\nNumber of words:" >> kitty_info.txt
cat kitty_ipsum_1.txt | wc -w >> kitty_info.txt
wc -m < kitty_ipsum_1.txt >> kitty_info.txt
grep meow kitty_ipsum_1.txt
grep --color meow kitty_ipsum_1.txt
grep --color -n meow kitty_ipsum_1.txt -- To show line numbers
grep --color -n meow[a-z]* kitty_ipsum_1.txt
grep -c meow[a-z]* kitty_ipsum_1.txt
grep -o meow[a-z]* kitty_ipsum_1.txt | wc -l
grep -o meow[a-z]* kitty_ipsum_1.txt | wc -l >> kitty_info.txt
echo -e "\nLines that they appear on:" >> kitty_info.txt
sed 's/r/2/' name.txt
sed 's/freecodecamp/f233C0d3C@mp/i' name.txt -- Ignores case
sed 's/freecodecamp/f233C0d3C@mp/i' < name.txt
cat name.txt | sed 's/freecodecamp/f233C0d3C@mp/i'
grep -n meow[a-z]* kitty_ipsum_1.txt | sed s/[0-9]/1/
grep -n meow[a-z]* kitty_ipsum_1.txt | sed -E s/[0-9]+/1/ -- Extended regex
grep -n meow[a-z]* kitty_ipsum_1.txt | sed -E 's/([0-9]+)/\1/' -- Replaces with same number
grep -n meow[a-z]* kitty_ipsum_1.txt | sed -E 's/([0-9]+)/\1/' -- To get numbers only in output
echo -e "\nNumber of times cat, cats, or catnip appears:" >> kitty_info.txt
grep -o 'cat[a-z]*' kitty_ipsum_1.txt
grep -o 'cat[a-z]*' kitty_ipsum_1.txt | wc -l >> kitty_info.txt
echo -e "\nLines that they appear on:" >> kitty_info.txt
grep -n 'cat[a-z]*' kitty_ipsum_1.txt | sed -E 's/([0-9]+).*/\1/' >> kitty_info.txt
echo -e "\n\n~~ kitty_ipsum_2.txt info ~~" >> kitty_info.txt
echo -e "\nNumber of lines:" >> kitty_info.txt
cat kitty_ipsum_2.txt | wc -l >> kitty_info.txt
echo -e "\nNumber of words:" >> kitty_info.txt
wc -w < kitty_ipsum_2.txt >> kitty_info.txt
wc -m < kitty_ipsum_2.txt >> kitty_info.txt
echo -e "\nNumber of times meow or meowzer appears:" >> kitty_info.txt
grep -o 'meow[a-z]*' kitty_ipsum_2.txt | wc -l >> kitty_info.txt
echo -e "\nLines that they appear on:" >> kitty_info.txt
grep -n 'meow[a-z]*' kitty_ipsum_2.txt | sed -E 's/([0-9]+).*/\1/' >> kitty_info.txt
grep --color 'cat[a-z]*' kitty_ipsum_2.txt
echo -e "\nNumber of times cat, cats, or catnip appears:" >> kitty_info.txt
grep -o 'cat[a-z]*' kitty_ipsum_2.txt | wc -l >> kitty_info.txt
echo -e "\nLines that they appear on:" >> kitty_info.txt
grep -n 'cat[a-z]*' kitty_ipsum_2.txt | sed -E 's/([0-9]+).*/\1/' >> kitty_info.txt
touch translate.sh
chmod +x translate.sh
#!/bin/bash
cat $1 | sed -E 's/catnip/dogchow/g; s/cat/dog/g; s/meow|meowzer/woof/g'
./translate.sh kitty_ipsum_1.txt
./translate.sh < kitty_ipsum_1.txt
cat kitty_ipsum_1.txt | ./translate.sh
sed 's/<pattern_1>/<replacement_1>/; s/<pattern_2>/<replacement_2>/'
./translate.sh kitty_ipsum_1.txt | grep --color dog[a-z]*
./translate.sh kitty_ipsum_1.txt | grep --color -E 'dog[a-z]*|woof[a-z]*'
./translate.sh kitty_ipsum_2.txt | grep --color -E 'meow[a-z]*|cat[a-z]*'
diff --color kitty_ipsum_1.txt doggy_ipsum_1.txt
[[ a =~ [0-9] ]]; echo $?
[[ a1 =~ [0-9] ]]; echo $?
The ^ signifies the start of the pattern, and $ means the end.
[[ a1 =~ ^[0-9]$ ]]; echo $?
[[ 11 =~ ^[0-9]+$ ]]; echo $? -- Allows to test more than 1 digit. Tests postitive integers.
sed s/<regex_pattern_to_replace>/<characters_to_replace_with>/<regex_flags>
echo '28 | Mountain' | sed 's/ /=/g' --replaces space with the chacters provided.
echo '28 | Mountain' | sed 's/ //g' -- removes space or regex characters.
echo '28 | Mountain' | sed 's/ |//'
echo "$(echo ' M e ' | sed 's/ //g')."
echo "$(echo ' M e ' | sed 's/^ //g')."
echo "$(echo ' M e ' | sed 's/^ *//g')."
echo "$(echo ' M e ' | sed 's/ $//g')."
echo "$(echo ' M e ' | sed 's/ $//g')."
echo "$(echo ' M e ' | sed 's/ *$//g')."
^ * will match all spaces at the beginning of text, and *$ will match spaces at the end
echo "$(echo ' M e ' | sed 's/^ *| *$//g')."
echo "$(echo ' M e ' | sed -E 's/^ *| *$//g')."
# Ignores case while searching.
grep -i STRING
# Can search multiple STRINGs.
grep -e STRING1 -e STRING2 ...
# Looks for exact STRING only.
grep -f STRING
# Remove comments from a file and spaces.
grep -v -e '^#' -e '^$' <file> | column -t
# To insert a special character among the elements of an array.
IFS=','
printf "%s\n" "${addr[*]}"| paste
getent services | grep -w 993
seinfo --type | grep -E -w passwd_[a-z].+
seinfo --type | grep -w -E http[a-z].+
# Prints the trailing directory only.
basename <path>
basename /home/root
# To remove escape ^M characters from all files.
for file in *; do
tr -d '\r' < "$file" > "${file}.temp"
mv "${file}.temp" "$file"
done
# Translating characters
tr '<>' ' ' < $CONTEXT_FILE | grep s_login_page | awk '{print $3}'
# To drop a certain column and return the remaining after that.
history | cut -d " " -f5-
# Same as above using the column number to drop.
history | cut --complement -d ' ' -f 3