简体   繁体   English

awk没有选择字符串和管道作为分隔符

[英]awk is not picking string and pipe as delimiter

I have a file where each record has a word "TST_BI|" 我有一个文件,其中每条记录都有一个单词"TST_BI|" and I need to use as a delimiter and populate the value after this string to a file . 我需要使用分隔符并将此字符串后的值填充到文件中。 There is only one occurrence of this string in each record. 每条记录中只出现一次此字符串。

It is working fine in AIX environment with below command. 它在AIX环境中使用以下命令正常工作。 awk -F "TST_BI|" '{print $2}' file.txt awk -F "TST_BI|" '{print $2}' file.txt . awk -F "TST_BI|" '{print $2}' file.txt

But when I migrated the code to Linux and tried the same, command is NOT working, where the value "|" 但是,当我移植的代码的Linux,并试图相同,命令无法正常工作,其中值"|" is also getting populated. 也正在人口稠密。 Below are the output from both AIX and Linux 以下是AIX和Linux的输出

Input : 输入:

<14>1 2017-08-31T04:13:47.2345839+00:00 loggregator ecsdasc0985-cs656-4asdsds-asds-asdasg6ds73 [DEV/2] - - TST_BI|DATE~2017-08-31 04:13:47,095|TIMESTAMP~04:13:47|TEST_ID~biTestExecutor-2|COUNTRY_CODE~XX|GROUP_TESTS~BZAG

OutPut from AIX : 来自AIX的OutPut:

DATE~2017-08-31 04:13:47,095|TIMESTAMP~04:13:47|TEST_ID~biTestExecutor-2|COUNTRY_CODE~XX|GROUP_TESTS~BZAG

With same command, Linux Output is 使用相同的命令, Linux输出

|DATE~2017-08-31 04:13:47,095|TIMESTAMP~04:13:47|TEST_ID~biTestExecutor-2|COUNTRY_CODE~XX|GROUP_TESTS~BZAG

A pipe is getting populated and which is not treating as delimiter. 管道正在填充,而不是作为分隔符处理。

Can anyone please help? 有人可以帮忙吗?

vertical bar char | 竖条char | specified with adjacent characters is treated as regex alternation operator/alternation group. 用相邻字符指定的值被视为正则表达式交替运算符/交替组。 In such case, to treat | 在这种情况下,要治疗| literally - it should be escaped \\\\| 从字面上看 - 它应该被转义为\\\\| or put into a character class [|] : 或者放入一个字符类[|]

awk -F'TST_BI[|]' '{print $2}' file.txt

The output: 输出:

DATE~2017-08-31 04:13:47,095|TIMESTAMP~04:13:47|TEST_ID~biTestExecutor-2|COUNTRY_CODE~XX|GROUP_TESTS~BZAG

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM