在特定列号之后忽略空格

时间:2017-07-10 14:51:34

标签: awk aix text-processing

嗨大家都有以下数据。

61684 376 23 106 38695633 1 0 0 -1 /C/Program Files (x86)/ 16704 root;TrustedInstaller@NT:SERVICE root;TrustedInstaller@NT:SERVICE 0 1407331175 1407331175 1247541608
8634 416 13 86 574126 1 0 0 -1 /E/KYCImages/ 16832 root;kycfinal@CGKYCAPP03 root;None@CGKYCAPP03 0 1406018846 1406018846 1352415392
60971 472 22 86 38613076 1 0 0 -1 /E/KYCwebsvc binaries/ 16832 root;kycfinal@CGKYCAPP03 root;None@CGKYCAPP03 0 1390829495 1390829495 1353370744
1 416 10 86 1 1 0 0 -1 /E/KycApp/ 16832 root;kycfinal@CGKYCAPP03 root;None@CGKYCAPP03 0 1411465772 1411465772 1351291187

现在我使用下面的代码:

awk 'BEGIN{FPAT = "([^ ]+)|(\"[^\"]+\")"}{print $10}' | awk '$1!~/^\/\./' | sort -u | sed -e 's/\,//g' | perl -p00e 's/\n(?!\Z)/;/g' filename

我收到了这个输出

/C/Program;/E/KycApp/;/E/KYCImages/;/E/KycServices/;/E/KYCwebsvc

但是我需要从10美元开始输出直到" /"再次遇到,基本上我想忽略第10列的任何空格直到" /"遇到了。 可能吗?

所需的输出

/C/Program Files (x86)/;/E/KycApp/;/E/KYCImages/;/E/KycServices/;/E/KYCwebsvc binaries/

2 个答案:

答案 0 :(得分:1)

使用单个 gawk

awk 'BEGIN{ FPAT="/[^/]+/[^/]+/"; PROCINFO["sorted_in"]="@ind_str_asc"; IGNORECASE = 1 }
     { a[$1] }END{ for(i in a) r=(r!="")? r";"i : i; print r }' filename

输出(不包含/E/KycServices/; - 因为它不在您的输入中):

/C/Program Files (x86)/;/E/KycApp/;/E/KYCImages/;/E/KYCwebsvc binaries/

答案 1 :(得分:0)

在单个awk中尝试关注。

awk '{match($0,/\/.*\//);VAL=VAL?VAL ORS substr($0,RSTART,RLENGTH):substr($0,RSTART,RLENGTH)} END{num=split(VAL, array,"\n");for(i=1;i<=num;i++){printf("%s%s",array[i],i==num?"":";")};print""}'  Input_file

将很快添加非单一衬里形式的解决方案,并提供解释。

EDIT1:现在也成功添加非单线形式的解决方案。

awk '{
        match($0,/\/.*\//);
        VAL=VAL?VAL ORS substr($0,RSTART,RLENGTH):substr($0,RSTART,RLENGTH)
     }
        END{
                num=split(VAL, array,"\n");
                for(i=1;i<=num;i++){
                                        printf("%s%s",array[i],i==num?"":";")
                                   };
                print""
           }
    '    Input_file

EDIT2:现在也在非单一形式的解决方案中添加代码说明。

awk '{
        match($0,/\/.*\//); ##Using match functionality of awk which will match regex to find the string in a line from / to \, note I am escaping them here too.
        VAL=VAL?VAL ORS substr($0,RSTART,RLENGTH):substr($0,RSTART,RLENGTH) ##creating a variable named VAL here which will concatenate its own value if more than one occurrence are there. Also RSTART and RSTART are the variables of built-in awk which will be having values once a match has TRUE value which it confirms once a regex match is found in a line.
     }
        END{ ##Starting this block here.
                num=split(VAL, array,"\n");##creating an variable num whose value will be number of elements in array named array, split is a built-in keyword of awk which will create an array with a defined delimiter, here it is new line.
                for(i=1;i<=num;i++){ ##Starting a for loop here whose value will go till num value from i variable value 1 to till num.
                                        printf("%s%s",array[i],i==num?"":";") ##printing the array value whose index is variable i and second string it is printing is semi colon, there a condition is there if i value is equal to num then print null else print a semi colon.
                                   };
                print"" ##print NULL value to print a new line.
           }
    '  Input_file    ###Mentioning the Input_file here.