Table of Contents
String Processing
Document some ways that Bash handles strings.
Replacement
Command/Syntax | Description |
---|---|
sed 's/regexp/replacement/' | Replace the first matched pattern. |
sed 's/regexp/replacement/g' | Replace all matched patterns. |
${Variable/regexp/replacement} | Replace the first matched pattern. |
${Variable//regexp/replacement} | Replace all matched patterns. |
Deletion
Command/Syntax | Description |
---|---|
${Variable%pattern} | Remove trailing matched pattern. |
${Variable#pattern} | Remove matched pattern from the head. |
Insertion
Addend 1
The line-number can also use the following special character:
$
- The last line.
Demo
Insert new line 0
before line 2.
cat test
line 1
line 2
line 3
sed -i "2i new line 0" test
cat test
line 1
new line 0
line 2
line 3
Insert new line 1
before the last line.
cat test
line 1
line 2
line 3
sed -i "\$i new line 1" test
cat test
line 1
line 2
new line 1
line 3