This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| sed [2025/07/01 00:36] – created ken | sed [2026/07/07 23:13] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ===== sed ===== | ===== sed ===== | ||
| - | Replacing multiple strings with sed | + | ==== Replacing multiple strings with sed ==== |
| While I'm the guy that effectively lives on the command-line, | While I'm the guy that effectively lives on the command-line, | ||
| Line 14: | Line 15: | ||
| sed --in-place=' | sed --in-place=' | ||
| </ | </ | ||
| + | \\ | ||
| + | \\ | ||
| + | \\ | ||
| + | ===== To find a line matching a specific pattern in a file, and append a value to the end of that line in Bash. ===== | ||
| + | |||
| + | The general syntax for this operation is:\\ | ||
| + | < | ||
| + | sed -i '/ | ||
| + | </ | ||
| + | Explanation: | ||
| + | |||
| + | sed -i:\\ | ||
| + | This option enables in-place editing, meaning the changes are directly applied to the original filename. If you want to see the changes before modifying the file, remove -i to print the output to standard output.\\ | ||
| + | / | ||
| + | This specifies the regular expression pattern to search for within each line. Only lines matching this pattern will be affected. | ||
| + | s/ | ||
| + | This is the substitution command:\\ | ||
| + | s: Indicates a substitution operation.\\ | ||
| + | $: This is a special regular expression character that matches the end of a line.\\ | ||
| + | value_to_append: | ||
| + | \\ | ||
| + | Example:\\ | ||
| + | To find lines containing " | ||
| + | < | ||
| + | sed -i '/ | ||
| + | </ | ||
| + | Using a variable for the value to append:\\ | ||
| + | You can also use a shell variable to store the value you want to append:\\ | ||
| + | < | ||
| + | append_value=" | ||
| + | sed -i "/ | ||
| + | </ | ||
| + | \\ | ||
| + | Note: When using variables in the substitution part of sed, it's important to use double quotes around the sed command to allow for variable expansion.\\ | ||
| + | Also, the $ in s/$/ needs to be escaped with a backslash (\$) when using double quotes to prevent the shell from interpreting it as a variable.\\ | ||