linux sed:用空格替换换行符< p>< /p>

oknrviil  于 2022-12-22  发布在  Linux
关注(0)|答案(3)|浏览(224)

假设我有这个文件:index.html
index.html

<p class="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>

我怎样才能把换行符替换成空格
仅标记
例如,将index.html更改为:

<p class="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>`

尝试过,

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

不工作

vngu2lb8

vngu2lb81#

使用gnu-sed

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

wixjitnu2#

使用你展示的示例,请尝试以下awk代码。在记录分隔符中使用awk的段落模式。

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
piv4azn7

piv4azn73#

使用sed(可能无法在所有平台上工作):sed -i.bak 's/\n$/ /g' index.html。请注意,sed将在此处执行更改之前备份您的文件。
可以使用tr代替sed,如下所示:tr '\n' ' ' .

相关问题