ubuntu:file:replace_all_occurrences_of_string_within_a_file
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
ubuntu:file:replace_all_occurrences_of_string_within_a_file [2019/11/27 21:29] – created peter | ubuntu:file:replace_all_occurrences_of_string_within_a_file [2022/06/13 11:38] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Ubuntu - File - Replace all occurrences of string within a file ====== | ||
- | |||
- | ===== Using awk ===== | ||
- | |||
- | Find and replace " | ||
- | |||
- | <code bash> | ||
- | awk ' | ||
- | </ | ||
- | |||
- | |||
- | Find and replace " | ||
- | |||
- | <code bash> | ||
- | awk ' | ||
- | </ | ||
- | |||
- | |||
- | Find cat by itself on it's own line even if there are spaces or tabs before it or after it. Replace it with dog. Then print the line. | ||
- | |||
- | <code bash> | ||
- | awk ' | ||
- | </ | ||
- | |||
- | |||
- | Substitute " | ||
- | |||
- | <code bash> | ||
- | awk ' | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Using sed ===== | ||
- | |||
- | Change all references of the word dog to cat in the file: | ||
- | |||
- | <code bash> | ||
- | sed -e ' | ||
- | </ | ||
- | |||
- | This uses **sed** to stream edit files, which means change them on the fly. | ||
- | |||
- | |||
- | Find every line that begins with cat. In that line replace furry with nothing. | ||
- | |||
- | <code bash> | ||
- | sed -i '/ | ||
- | </ | ||
- | |||
- | |||
- | |||
- | Find any line starting with the defined shell variable SHELLVAR (notice ' ' around it so it's evaluated). | ||
- | |||
- | <code bash> | ||
- | sed -i '/ | ||
- | </ | ||
- | |||
- | |||
ubuntu/file/replace_all_occurrences_of_string_within_a_file.1574890149.txt.gz · Last modified: 2020/07/15 09:30 (external edit)