在换行符替换 [英] Replace newlines in

查看:175
本文介绍了在换行符替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些Linux XML文件

  A =XX
XX
XX
B =Y
ÿ
Y

如何更换的内容和变

  A =XX \\ n \\ XX \\ NXX
B =Y
ÿ
Y

其中的属性B不是替换

我试试这个,但如何支持多文件,并替换属性一只用awk或者sed命令; (未使用XSLT)

  sed的':一个; N; $ BA; S / \\ N / \\\\ñ/ g的!abc.xml


解决方案

gawk的

  GAWK -vRS =\\ NB =/ A = / {GSUB(\\ n,\\\\ N)}
{如果(RT ==)的printf%S,$ 0个
  其他印刷
}'ORS =\\ NB =文件

输出

  $ ./shell.sh
A =XX \\ NXX \\ NXX
B =Y
ÿ
Y

i have some xml files in linux

a="xx
xx
xx"
b="y
y
y"

How to replace the content and becomes

a="xx\n\xx\nxx"
b="y
y
y"

where attribute b is not replace

i have try this but how to support multi files and replace the attribute "a" only using sed or awk command; (not use xslt)

sed ':a;N;$!ba;s/\n/\\n/g' abc.xml

解决方案

gawk

gawk -vRS="\nb=" '/a=/{ gsub("\n","\\n") }
{ if (RT == "") printf "%s", $0
  else print
}' ORS="\nb=" file

output

$ ./shell.sh
a="xx\nxx\nxx"
b="y
y
y"

这篇关于在换行符替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆