# can delete a sub-string from the very beginning , like var=apple; echo ${var#app}, which will print le.
#, delete a few things as posible
##, (greedy)delete a more things as posible
%, use like #, but it delete from the end
echo ${var%el}, print app
shebang
a shebang is the interpreter of this script. like this
1
#!/bin/sh
Redirection
command
function
a > b
a’s std output override into b
a >> b
a’s std output appand into b
a < b
b as a’s input
a << b
here document
a <<< b
here
Here document
A block as input
1 2 3 4 5 6 7 8 9 10
echo << token text token
example: echo << __EOF__ halo __EOF__
$ halo
Here
a bit like here document, a line as input.
1 2 3
read ans1 ans2 ans3 <<< "a1 a2 a3"
a1 a2 a3 as input to read process
Useful commands
Grep
grep [option] file1 file2...,support base regular expression,egrep or grep -E support extended Regex
The most commonly used | options | description | |———-|——————————–| | -i | –ignore-case | | -v | –revert-mathch | | -c | –count | | -l | –file-with-match | | -L | –file-without-math | | -n | –line-number | | -h | no filename in multi file mode | | -a/-text | do not ignore binary data | | -e | –regexp | | -f | specify regexp file |
Sed
sed can deal with text file with script-command or script-file.
sed [options][target-file]
options
description
-e
<++>
<++>
<++>
<++>
<++>
<++>
<++>
<++>
<++>
<++>
<++>
<++>
script action
action
description
a\
add a new line or lines next to current line
c\
replace current line with some text
d
delete line
i\
insert text befor curent line
h
<++>
H
<++>
g
<++>
G
<++>
I
<++>
p
print line
n
<++>
q
quit sed
r
<++>
!
<++>
s
replace string with other string
<++>
<++>
<++>
<++>
<++>
<++>
<++>
<++>
<++>
<++>
replace identifier
options
description
g
global replace in this line
p
print line
w
write line into file
x
<++>
y
<++>
<++>
<++>
Cut
options
description
<++>
<++>
<++>
<++>
<++>
<++>
<++>
<++>
<++>
<++>
<++>
<++>
<++>
<++>
Regular Expressions
Literals
pattern
description
.
match any char
char
literaly a char
Metacharacters
Anchors
pattern
description
^
found at the begin of line
$
found at the end of line
Quantifiers
pattern
description
?
match an element zero or one time
*
match an element zero or more times
+
match an element one or more times
{}
match an element a specific number of times
{n,}
match an element if it occurs n or more times
{,n}
match an element if it occurs no more than n times
A quantifiers is after an element, such as .* means match any char with any lengh equivalent any string.
An element can like this [A_Z]and this[:digit:]. We can see the pattern before a quantifiers as a whole.
Bracket Expressions and Character Classes
Bracket Expressions
specify a set of characters to be match
1 2 3 4
$ grep -h '[bg]zip' test.txt bzip2 bzip123 gzip
Negation
If the first char in a bracket expression is a caret(^), the remaining char are taken to be a set of chars that must not be present at the given char position.