简体   繁体   English

使用 sed 或 awk 将字符串分成 3 列

[英]divide the string into 3 columns using sed or awk

I have the following lines in Jenkins console outpu along with several other text.我在 Jenkins 控制台输出中有以下几行以及其他几个文本。 I wanted to split the following text lines into 3 columns as shown below, and rest of the text lines in console output are not required..我想将以下文本行拆分为 3 列,如下所示,并且不需要控制台输出中的其余文本行。

NumberOfTestCases tier1 in https://****:****@bess-bitbucket.bearingpoint.com/scm/bnktst/stat-autopilot-testcases.git are 4 
NumberOfTestCases tier2 in https://****:****@bess-bitbucket.bearingpoint.com/scm/bnktst/stat-autopilot-testcases.git are 4 
NumberOfTestCases tier3 in https://****:****@bess-bitbucket.bearingpoint.com/scm/bnktst/stat-autopilot-testcases.git are 4 
TOPDIR托普迪尔 URL网址 NumberOfTestCases测试用例数
tier1第一层 https:// : @bess-bitbucket.bearingpoint.com/scm/bnktst/stat-autopilot-testcases.git https:// @bess-bitbucket.bearingpoint.com/scm/bnktst/stat-autopilot-testcases.git 4 4
tier2 2级 https:// : @bess-bitbucket.bearingpoint.com/scm/bnktst/stat-autopilot-testcases.git https:// @bess-bitbucket.bearingpoint.com/scm/bnktst/stat-autopilot-testcases.git 4 4
tier3三级 https:// : @bess-bitbucket.bearingpoint.com/scm/bnktst/stat-autopilot-testcases.git https:// @bess-bitbucket.bearingpoint.com/scm/bnktst/stat-autopilot-testcases.git 4 4

I tried as following, am confused how to add the first column info:我尝试如下,我很困惑如何添加第一列信息:

wget ${BUILD_URL}/consoleText -O ${WORKSPACE}/SomeFile.txt


sed -n 's/.*\(https.*git\)\ are\ \([[:digit:]]\{1,\}\)/\1,\2/p'

Using GNU sed you might delete unwanted parts then change spaces to , (or any separator you wish to use), let file.txt content be使用 GNU sed您可能会删除不需要的部分,然后将空格更改为, (或您希望使用的任何分隔符),让file.txt内容为

NumberOfTestCases tier1 in https://****:****@bess-bitbucket.bearingpoint.com/scm/bnktst/stat-autopilot-testcases.git are 4 
NumberOfTestCases tier2 in https://****:****@bess-bitbucket.bearingpoint.com/scm/bnktst/stat-autopilot-testcases.git are 4 
NumberOfTestCases tier3 in https://****:****@bess-bitbucket.bearingpoint.com/scm/bnktst/stat-autopilot-testcases.git are 4

then然后

sed -e 's/NumberOfTestCases //' -e 's/ in / /' -e 's/ are / /' -e 's/ *$//g' -e 's/ /,/g' file.txt

gives output给出输出

tier1,https://****:****@bess-bitbucket.bearingpoint.com/scm/bnktst/stat-autopilot-testcases.git,4
tier2,https://****:****@bess-bitbucket.bearingpoint.com/scm/bnktst/stat-autopilot-testcases.git,4
tier3,https://****:****@bess-bitbucket.bearingpoint.com/scm/bnktst/stat-autopilot-testcases.git,4

Explanation: I remove parts which are not desired in output: NumberOfTestCases, in, are, then remove traling spaces ( *$ ) then replace all spaces using , (feel free to adjust to any character you need in output).说明:我删除了输出中不需要的部分:NumberOfTestCases, in, are,然后删除 traling 空格 ( *$ ) 然后使用,替换所有空格(随意调整为输出中需要的任何字符)。 Disclaimer : this solution assumes that there is never space inside URL.免责声明:此解决方案假定 URL 内永远没有空格。

(tested in GNU sed 4.2.2) (在 GNU sed 4.2.2 中测试)

mawk 'sub(".",_,$!(NF=NF))' OFS=, FS='(^[^ ]+| (in|are)) '
tier1,https://****:****@bess-bitbucket.bearingpoint.com/scm/bnktst/stat-autopilot-testcases.git,4 
tier2,https://****:****@bess-bitbucket.bearingpoint.com/scm/bnktst/stat-autopilot-testcases.git,4 
tier3,https://****:****@bess-bitbucket.bearingpoint.com/scm/bnktst/stat-autopilot-testcases.git,4 

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

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