awk:如果其他字段匹配值,则打印字段范围

时间:2019-11-04 12:10:30

标签: file parsing awk printf

我有一个格式很旧的文件。这是几行示例:

000000582103145338520001        2000111420040924NR19 2RG195006          0119MR<PATRICK JOSEPH ROBERT<SNOWBALL<<<<THE OLD RECTORY<LONGHAM<EAST DEREHAM<NORFOLK<<INSURANCE COMPANY OFFICIAL<BRITISH<<
000000582103015819370001        1994010119981130CR2 8SZ 194205          0096MR<PETER GEOFFREY<WARD<<<<14 SUFFIELD CLOSE<SELSDON<SOUTH CROYDON<<<EXECUTIVE DIRECTOR<ENGLISH<<
000000582203047002770001        1992012619931231N1 8HP  193401          0099<JOHN HOWARD<WEBB<<<<1 SUDELEY STREET<ISLINGTON<LONDON<<<GROUP ACTUARY - COMMERCIAL UNION<BRITISH<<
000000582103000497250003        1998070119981130TN13 1SS195207          0126MR<RICHARD ANDREW<WHITAKER<LLB DMS FCII<<<STRATHBLANE ASHGROVE ROAD<<SEVENOAKS<KENT<<COMPANY SECRETARY<BRITISH<UNITED KINGDOM<
000000781D                      00000020WALKER & ETH PORKER<
000000831D                      00000014REID AND SONS<
000000841D                      00000019A. WEST & PARTNERS<
000000861                       00130029KENTSTONE PROPERTIES LIMITED<

仅当第9个字符为1时,我才尝试从第41个字符开始直到行尾。我知道char 41之后的最大字符数为161。

这是我的awk,它会中断(主要是试图从网上找到的其他代码中编写它-此处不是awk专家)。

awk -v b=41 -v e=201 
'$9 == "1" 
BEGIN{FS=OFS=""} {for (i=b;i<=e;i++) 
printf "%s%s", $i, (i<e ? OFS : ORS)}' 
<(head -n1000 myfile.dat)

我希望代码输出什么:

WALKER & ETH PORKER<                                                                                                                                             
REID AND SONS<                                                                                                                                                   
A. WEST & PARTNERS<                                                                                                                                              
KENTSTONE PROPERTIES LIMITED<    

2 个答案:

答案 0 :(得分:3)

请您尝试以下。

awk 'substr($0,9,1) == 1{print substr($0,41)}' Input_file

说明:

awk '                     ##Starting awk program here.
substr($0,9,1) == 1{      ##Using substr for getting sub-string from 9th character to get only 1 character and checking condition if its value is equal to 1. If condition is TRUE then perform following.
  print substr($0,41)     ##Printing sub-string value from 41st character to till end of line(since no last limit is given so it will take complete line from 41st character).
}                         ##Closing BLOCK for condition here.
' Input_file              ##Mentioning Input_file name here.

答案 1 :(得分:1)

Ravinders帖子的一小部分变化。 (gnu awk)

Scanner robo = new Scanner (System.in);
System.out.println("Enter  a word ");
String word = robo.nextLine();
String textInvert = "";
int indexAnt = 0;
for (int i = 0; i < word.length(); i++) {
    if (Character.isUpperCase(word.charAt(i))) {
        String wordSplit = word.substring(indexAnt, i + 1);
        for (int j = wordSplit.length() - 1; j >= 0; j--)
            textInvert += wordSplit.charAt(j);
        indexAnt = i + 1;
    }
}
System.out.println(textInvert);

有关awk -v FS= '$9==1 {print substr($0,41)}' file WALKER & ETH PORKER< REID AND SONS< A. WEST & PARTNERS< KENTSTONE PROPERTIES LIMITED< 的帮助,请参阅: https://www.gnu.org/software/gawk/manual/html_node/String-Functions.html