3

I am using sed to remove the new line and replace with <br> but I am not able to get the desired output.

I wrote:

find . -name $1 -print0 | xargs -0 sed -i '' -e 's|\n|ABC|g'

...but this doesn't work.

agc
  • 548
  • 4
  • 13
abhijeetmote
  • 43
  • 1
  • 6
  • possible duplicate of [Find and replace text within a file using commands](http://askubuntu.com/questions/20414/find-and-replace-text-within-a-file-using-commands) – Pandya Sep 26 '14 at 16:01
  • You can invoke programs on `find` results a bit more elegantly with the `-exec` action: `find . -name $1 -exec sed -i '' -e 's|\n|ABC|g' \{\} +` – David Foerster Sep 26 '14 at 16:12
  • @Pandya I don't believe this is a duplicate, the OP is already using what that question would suggest, he just needs help getting the exact command for his specific environment correct, which that question won't provide. – Seth Sep 26 '14 at 16:19
  • 1
    You can use `tr '\n' '
    ' < file`
    – Pandya Sep 27 '14 at 02:14

4 Answers4

2

Your sed expression is treating each line separately - so it doesn't actually read the newline character into the pattern buffer and hence can't replace it. If you just want to add <br> while retaining the actual newline as well, you can just use the end-of-line marker $ and do

sed -i'' 's|$|<br>|' file

Note that the empty backup file name - if you use it - must directly follow the -i like -i''; also the -e is not necessary when using a single expression.

OTOH if you really want to replace actual newline characters, you need to jump through some extra hoops, for example

sed -i'' -e :a -e '$!N;s/\n/<br>/;ta' -e 'P;D' file

or, more compactly

sed -i'' ':a; $!N; s|\n|<br>|; ta; P;D' file

which read successive pairs of lines into the pattern buffer and then replace the intervening newline - see Famous Sed One-Liners Explained.

steeldriver
  • 131,985
  • 21
  • 239
  • 326
0

If you want to replace the new line and with <br>, you can use

 find . -name $1 -print0 | xargs -0 sed -i 's/.*$/&<br\>/'
g_p
  • 18,154
  • 6
  • 56
  • 69
0

Why not just

find . -name $1 | xargs sed -ri "s/$/<br>/"

?

(Try without the i, first ;-) )

laruiss
  • 111
  • 2
0

To replace inline \n with <br>, just use perl (or sed), needless to call find for that task:

perl -pi -e "s/$/<br>/" myfile

Or for an alias:

alias brtag='perl -pi -e "s/$/<br>/" $1'
Sylvain Pineau
  • 61,564
  • 18
  • 149
  • 183