Home > Enterprise >  sed: replace newline within <p> </p> with space
sed: replace newline within <p> </p> with space

Time:12-20

Let's say I have the file: index.html

index.html

<p >This
entry will determine the number of transmit threads, each channel
represents one thread, which can handle multiple multicast ports.
All channels are enabled by default. To temporarily disable a particular
channel, use the folowing configuration lines:</p>

How can i replace newline to space within

tag only

For example, changing index.html to:

<p >This entry will determine the number of transmit threads, each channel represents one thread, which can handle multiple multicast ports. All channels are enabled by default. To temporarily disable a particular channel, use the folowing configuration lines:</p>`

Tried with,

sed -e "/<p>/s/\n/ /g" index.html

not working

CodePudding user response:

Using gnu-sed:

sed -E '/<p >/{:a; N; s/\n/ /g; /<\/p>/!ba;}' index.html

CodePudding user response:

With your shown samples please try following awk code. Using awk's paragraph mode in Record separator.

awk -v RS=  '
match($0,/(^|\n)\<p .*\<\/p\>/){
  val=substr($0,RSTART,RLENGTH)
  gsub(/\n/," ",val)
  print substr($0,1,RSTART-1) val substr($0,RSTART RLENGTH)
}
'  Input_file

CodePudding user response:

With sed (which may not work on all platforms): sed -i.bak 's/\n$/ /g' index.html. Note that sed will backup your file before performing changes here.

Instead of sed, you can use the tr as follow: tr '\n' ' '.

  • Related