AIX(没有GNU sed / awk)连接行,如果它在末尾没有控制M或\ r \ n字符

时间:2022-12-01 15:28:33

I am looking for way to join line if the file does not control M character. AIX has its standard awk and sed utility but not GNU

如果文件不控制M字符,我正在寻找加入线的方法。 AIX有标准的awk和sed实用程序,但不是GNU

The issue: we get file from 3rd party , from windows. And the file has ^M (i.e. \r) character at end of each line, expect for some lines , in which the data in some field itself has \n character. Hence there is need to join the lines which has this extra \n character.

问题:我们从第三方获取文件,来自Windows。并且该文件在每行的末尾具有^ M(即\ r)字符,期望某些行,其中某些字段中的数据本身具有\ n字符。因此需要加入具有这个额外\ n字符的行。

Data example :

数据示例:

col1|col2|col3|col4|col5|^M
a1|a2|a3|a4|a5|^M
b1|b2|b3|b
4|b5|^M
c1|c2|c3|c4|c5|^M

expected output.

预期产出。

col1|col2|col3|col4|col5|^M
a1|a2|a3|a4|a5|^M
b1|b2|b3|b4|b5|^M
c1|c2|c3|c4|c5|^M

Thank you in advance for any help.

预先感谢您的任何帮助。

2 个解决方案

#1


2  

Just for the record, perl is handling really good transformations of \n, \r , etc, without the restrictions of non gnu sed - actually perl -pe can replace sed directly.

仅仅为了记录,perl正在处理\ n,\ n \ r \ n等非常好的转换,没有非gnu sed的限制 - 实际上perl -pe可以直接替换sed。

So this operation worked fine in BSD :

所以这个操作在BSD中运行良好:

$ echo -ne "abc\r\ndef\nijk\r\nlmn\r\n" |cat -vte
abc^M$
def$
ijk^M$
lmn^M$

$ echo -ne "abc\r\ndef\nijk\r\nlmn\r\n" |perl -pe "s/\r\n/\0/g;s/\n//g;s/\0/\r\n/g" |cat -vte
abc^M$
defijk^M$
lmn^M$

#2


0  

A literal carriage-return can be used by typing ^V (Ctrl-V) followed by the "Return" key.

键入^ V(Ctrl-V)后跟“返回”键可以使用文字回车。

The following sed(1) script loops over lines that do not end in a carriage-return, removing the undesired line-feeds:

以下sed(1)脚本循环遍历不在回车符中的行,删除不需要的换行符:

sed '
:label
/^M$/! {
    N
    s/\n//
    blabel
}'

As one line:

作为一行:

sed -e ':l' -e '/^M$/!{N;s/\n//;bl' -e '}'

#1


2  

Just for the record, perl is handling really good transformations of \n, \r , etc, without the restrictions of non gnu sed - actually perl -pe can replace sed directly.

仅仅为了记录,perl正在处理\ n,\ n \ r \ n等非常好的转换,没有非gnu sed的限制 - 实际上perl -pe可以直接替换sed。

So this operation worked fine in BSD :

所以这个操作在BSD中运行良好:

$ echo -ne "abc\r\ndef\nijk\r\nlmn\r\n" |cat -vte
abc^M$
def$
ijk^M$
lmn^M$

$ echo -ne "abc\r\ndef\nijk\r\nlmn\r\n" |perl -pe "s/\r\n/\0/g;s/\n//g;s/\0/\r\n/g" |cat -vte
abc^M$
defijk^M$
lmn^M$

#2


0  

A literal carriage-return can be used by typing ^V (Ctrl-V) followed by the "Return" key.

键入^ V(Ctrl-V)后跟“返回”键可以使用文字回车。

The following sed(1) script loops over lines that do not end in a carriage-return, removing the undesired line-feeds:

以下sed(1)脚本循环遍历不在回车符中的行,删除不需要的换行符:

sed '
:label
/^M$/! {
    N
    s/\n//
    blabel
}'

As one line:

作为一行:

sed -e ':l' -e '/^M$/!{N;s/\n//;bl' -e '}'