Issue
I know that I can add space after or before a character using
sed -i 's/char/char /g' fname.txt
However, I need help adding space before and after these characters ```[]<>'''
for example,
nie[breath]hi
hi </langenglish>'tjie
would turn into
nie [breath] hi
hi </langenglish> 'tjie
NOTE: that i want a single space after <>[]
because using the first code, I would essentially get two spaces.
Please let me know how I could accomplish this task.
Solution
From your example it seems like you're mis-specifying your problem and you actually want a space before and after every [...]
or <...>
string rather than before and after every [
, ]
, <
, or >
character. If that's correct then using any sed that allows \n
to represent a newline in the regexp and replacement text (e.g. GNU sed):
$ sed 's/ /\n/g; s/\[[^]]*]/ & /g; s/<[^>]*>/ & /g; s/ *\n */ /g' file
nie [breath] hi
hi </langenglish> 'tjie
or with any awk:
$ awk '{gsub(/ /,"\n"); gsub(/\[[^]]*]/," & "); gsub(/<[^>]*>/," & "); gsub(/ *\n */," ")} 1' file
nie [breath] hi
hi </langenglish> 'tjie
You should add some non-sunny-day test cases to your example to make sure you get what you really need, e.g.:
$ echo 'head[foo<bar]tail' | awk '{gsub(/ /,"\n"); gsub(/\[[^]]*]/," & "); gsub(/<[^>]*>/," & "); gsub(/ *\n */," ")} 1'
head [foo<bar] tail
$ echo 'head<foo<bar>tail' | awk '{gsub(/ /,"\n"); gsub(/\[[^]]*]/," & "); gsub(/<[^>]*>/," & "); gsub(/ *\n */," ")} 1'
head <foo<bar> tail
$ echo 'head<foo[bar>tail' | awk '{gsub(/ /,"\n"); gsub(/\[[^]]*]/," & "); gsub(/<[^>]*>/," & "); gsub(/ *\n */," ")} 1'
head <foo[bar> tail
$ echo 'head<foo]bar>tail' | awk '{gsub(/ /,"\n"); gsub(/\[[^]]*]/," & "); gsub(/<[^>]*>/," & "); gsub(/ *\n */," ")} 1'
head <foo]bar> tail
$ echo 'head<foo[bar]tail' | awk '{gsub(/ /,"\n"); gsub(/\[[^]]*]/," & "); gsub(/<[^>]*>/," & "); gsub(/ *\n */," ")} 1'
head<foo [bar] tail
Answered By - Ed Morton