1

我正在尝试填充一些 XML 字段,以便让自己在我正在执行的大量手动数据输入中占得先机。

我正在尝试使用正则表达式和 2 个平面文件来转换这样的文本:

EA10A3

进入这个:

<nodevice id="EA10A3" seq=" " />

以下是 sed 发生的情况:

~/Tickets/RWSP-11422 > cat locations.txt
EA10A1
EA10A2
EA10A3

~/Tickets/RWSP-11422 > cat platform
  <nodevice id="PPPPPP" seq=" " />

~/Tickets/RWSP-11422 > while read i; do cat platform | sed "s/PPPPPP/$i/g"; done <locations.txt
" seq=" " />ice id="EA10A1
" seq=" " />ice id="EA10A2
" seq=" " />ice id="EA10A3

Perl 也是如此:

~/Tickets/RWSP-11422 > while read i; do cat platform | perl -wpl -e "s/PPPPPP/$i/g"; done < locations.txt
" seq=" " />ice id="EA10A1
" seq=" " />ice id="EA10A2
" seq=" " />ice id="EA10A3

为什么我越来越

" seq=" " />ice id="EA10A1 

代替

<nodevice id="EA10A1" seq=" " />

?

我怎样才能做到这一点?

4

3 回答 3

7

你得到

<nodevice id="EA10A1^M" seq=" " />

因为您的输入文件包含

EA10A1^M^J

使用 (eg) 修复您的输入文件dos2unix

^M代表回车,^J代表换行。)

于 2013-09-20T15:27:52.723 回答
3

看起来locations.txt有 DOS 行尾 ( \r\n) 而不是 UNIX 行尾 ( \n)。当打印回车时\r,光标移动到行首,导致输出混乱。

您可以使用 来验证这一点cat -v,它将回车符打印为^M

$ cat -v locations.txt
EA10A1^M
EA10A2^M
EA10A3^M
$ while read i; do cat platform | sed "s/PPPPPP/$i/g"; done < locations.txt | cat -v
  <nodevice id="EA10A1^M" seq=" " />
  <nodevice id="EA10A2^M" seq=" " />
  <nodevice id="EA10A3^M" seq=" " />

运行或修复它locations.txtdos2unixfromdos

$ dos2unix locations.txt
dos2unix: converting file blah.txt to Unix format ...
$ cat -v locations.txt
EA10A1
EA10A2
EA10A3
$ while read i; do cat platform | sed "s/PPPPPP/$i/g"; done < locations.txt | cat -v
  <nodevice id="EA10A1" seq=" " />
  <nodevice id="EA10A2" seq=" " />
  <nodevice id="EA10A3" seq=" " />
于 2013-09-20T15:28:15.023 回答
2

对我来说似乎是个dos2unix问题。尝试运行

 dos2unix locations.txt
 perl -ne 'chomp ; printf ("<nodevice id=\"$_\" seq=\" \" / > \n");' locations.txt
于 2013-09-20T15:30:22.600 回答