Tytire Recubans
Tytire Recubans

Reputation: 997

awk: print range of fields if other field matches value

I have a file with a very old format. Here's a couple of lines of examples:

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<

I am trying to get the characters from 41st till the end of the line if and only if the 9th character is a 1. I know that the max number of chars after char 41 is 161.

Here's my awk - which breaks (mainly tried to compose it from different code found online - not an awk expert here).

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)

What I expect the code to output:

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

Upvotes: 0

Views: 222

Answers (2)

RavinderSingh13
RavinderSingh13

Reputation: 133458

Could you please try following.

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

Explanation:

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.

Upvotes: 3

Jotne
Jotne

Reputation: 41456

A small variation of Ravinders post. (gnu awk)

awk -v FS= '$9==1 {print substr($0,41)}' file
WALKER & ETH PORKER<
REID AND SONS<
A. WEST & PARTNERS<
KENTSTONE PROPERTIES LIMITED<

For help with substr, see: https://www.gnu.org/software/gawk/manual/html_node/String-Functions.html

Upvotes: 1

Related Questions