For example if I have a file like
[email protected], yo@
gmail.com yo@gmail
.com
And I want to replace the string [email protected]
.
If the file had the target string in a single line then we could've just used
sed "s/[email protected]/[email protected]/g" file
So, is it possible for me to catch patters that are spread between multiple line without replacing the \n?
Something like this
[email protected], e@
email.com e@email
.com
Thank you.
CodePudding user response:
You can do this:
tr -d '\n' < file | sed 's/[email protected]/[email protected]/g'
CodePudding user response:
This might work for you (GNU sed):
sed -E 'N;s/yo@gmail\.com/[email protected]/g
h;s/(\S )\n(\S )/\1\2\n\1/;/yo@gmail\.com/!{g;P;D}
s//\[email protected]/;:a;s/\n(\S)(\S )\n\S/\1\n\2\n/;ta
s/(.*\n.*)\n/\1/;P;D' file
Append the following line to the pattern space.
Replace all occurrences of matching email address in both the first and second lines.
Make a copy of the pattern space.
Concatenate the last word of the first line with the first word of the second and keep the second line as is. If there is no match with the email address, revert the line, print/delete the first line and repeat.
Otherwise, replace the match and re-insert the newline as of the length of the first word of the second line (deleting the first word of the second line too).
Remove the newline used for scaffolding, print/delete the first line and repeat.
N.B. The lines will not be of the same length as the originals if the replacement string length does not match the matching string length. Also there has been no attempt to break the replacement string in the same relative split if the match and replacement strings are not the same length.