bash:files:find_replace_a_string_within_a_file
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
bash:files:find_replace_a_string_within_a_file [2021/02/10 16:05] – created peter | bash:files:find_replace_a_string_within_a_file [2021/02/10 16:14] (current) – [Put the output of file f1 into the pattern space of file f2] peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== BASH - Files - Find & Replace a String within a File ====== | ====== BASH - Files - Find & Replace a String within a File ====== | ||
+ | |||
+ | See: [[BASH: | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Changing contents within a file ===== | ||
+ | |||
+ | <code bash> | ||
+ | contents=$(< | ||
+ | $ echo " | ||
+ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | |||
+ | * **infile.txt**: | ||
+ | * **$old**: | ||
+ | * **$new**: | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | To change the file in-place: | ||
+ | |||
+ | <code bash> | ||
+ | echo " | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Put the output of file1 into the pattern space of file2 ===== | ||
+ | |||
+ | <file text file1> | ||
+ | aaaaaaaaaaaaaaaaaa //these line go in file2 | ||
+ | aaaaaaaaaaaaaaaaaa | ||
+ | aaaaaaaaaaaaaaaaaa | ||
+ | </ | ||
+ | |||
+ | <file text file2> | ||
+ | Pattern_start | ||
+ | __________ //these are the line to be replaced | ||
+ | __________ | ||
+ | Pattern_end | ||
+ | </ | ||
+ | |||
+ | <code bash> | ||
+ | sed -n '/ | ||
+ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | |||
+ | * **< | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | If the lines between Pattern_start and Pattern_end contain only hyphens then you can use this: | ||
+ | |||
+ | <code bash> | ||
+ | sed -n '/ | ||
+ | </ | ||
bash/files/find_replace_a_string_within_a_file.1612973157.txt.gz · Last modified: 2021/02/10 16:05 by peter