在 awk 输出中打印特殊字符

在 awk 输出中打印特殊字符

我有一个大文件,其中包含大量带有 RFH2 标头的 MQ 消息。文件中的每条消息均由空行分隔。现在我需要将这个大文件分割成小文件,每个小文件包含带有 RFH2 标头的单个消息。

我尝试使用下面的 awk 命令

awk '{RS=""} {print $0}' inputfile

这会打印没有控制字符的第一行,这是没有用的。第一个 MQ RFH 标题行的开头类似于RFH ^B^C^X^A^Q^C3MQSTR ^D¸消息数据。 awk 输出仅打印文本RFH。如果运行此命令后输入文件有 50 条消息,我会得到 50 个仅包含文本的文件RFH。我期待 50 个带有 RFH2 标头和数据的文件。

我无法为您提供真实的文件输入,因为它包含敏感数据。该文件开头为

RFH ^B^C^X^A^Q^C3MQSTR ^D¸X<jms>
.........some text of many lines.....

RFH ^B^C^X^A^Q^C3MQSTR ^D¸X<jms>
........some text of many lines.....

RFH ^B^C^X^A^Q^C3MQSTR ^D¸X<jms>
...

输出文件应该有

RFH ^B^C^X^A^Q^C3MQSTR ^D¸X<jms>
.........some text of many lines

答案1

干得好。输入(测试文件):

RFH ^B^C^X^A^Q^C3MQSTR ^D¸X<jms>
.........some text of many lines.....
.........some text of many lines.....
.........some text of many lines.....
.........some text of many lines.....
.........some text of many lines.....

RFH ^B^C^X^A^Q^C3MQSTR ^D¸X<jms>
........some text of many lines.....
.........some text of many lines.....
.........some text of many lines.....
.........some text of many lines.....
.........some text of many lines.....
.........some text of many lines.....

RFH ^B^C^X^A^Q^C3MQSTR ^D¸X<jms>
.........some text of many lines.....
.........some text of many lines.....
.........some text of many lines.....
.........some text of many lines.....

代码:

awk '{print $0 > "file" NR}' RS='\n\n' testfile

将“文件”替换为您想要的文件的名称。通过这个例子,您将拥有:

$ cat file1
RFH ^B^C^X^A^Q^C3MQSTR ^D¸X<jms>
.........some text of many lines.....
.........some text of many lines.....
.........some text of many lines.....
.........some text of many lines.....
.........some text of many lines.....

$ cat file2
RFH ^B^C^X^A^Q^C3MQSTR ^D¸X<jms>
........some text of many lines.....
.........some text of many lines.....
.........some text of many lines.....
.........some text of many lines.....
.........some text of many lines.....
.........some text of many lines.....

$ cat file3
RFH ^B^C^X^A^Q^C3MQSTR ^D¸X<jms>
.........some text of many lines.....
.........some text of many lines.....
.........some text of many lines.....
.........some text of many lines.....

答案2

这很接近:

awk '{RS=""} {print $0}' inputfile

但是,您需要定义 RS 变量awk 开始读取文件。选择以下之一:

awk 'BEGIN {RS=""} {print}' inputfile
awk -v RS="" '{print}' inputfile

要查看控制字符,请将 awk 输出通过管道传输到cat -v

awk -v RS="" 1 inputfile | cat -v

相关内容