Link to home
Start Free TrialLog in
Avatar of glennstewart
glennstewart

asked on

Simply awk/sed solution required - Split a file on fields

Hi All,

Trying to create a nice easy addition to a script using sed or awk to split a file with a given field.

---------------------------
INPUT FILE:


||AU|123456|E000|0212345678|email@domain.com|
TEXT
||US|123457|E567|0212344286||
TEXT
||AU|123456|E098|0212347129||
TEXT
||UK|123458|E123|0212347532|email@domain.com|
TEXT
---------------------------

This file needs to be split into multiple files. The two consecutive pipes ("||") will be considered to be the start of a new file. On line preceded by ||, the line contains multiple fields, the last of which is optional (email field).

Optimal solution will:

a) Use either sed, awk, a combination or ksh supported commands
b) Will be a short and as simple as possible
c) Will create files using the first and second fields as part of the filename and and increment (e.g. AU123456_001, US123457_002,AU123456_003,UK123458_004)

Thanks in advance,

Glenn
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rochit85
rochit85

hi ozo
please could you explain the solution you gave. i am also facing same problem.

Just a reminder:

This post looks like homework, please reread out member agreement: https://www.experts-exchange.com/jsp/infoMemberAgreement.jsp

Homework isn't allowed !
Avatar of glennstewart

ASKER

Hi MikeOM_DBA,

Certainly not homework ;) It's certainly been a couple of decades since I've needed to complete homework (thankfully). Completely understand where you're coming from though.

This is a case of pressed for time. I also know whenever I provide the members with a challenge, they always love stepping up to the plate. Experts Exchange has always been a fantastic resource for questions like this for me.

Regards,

Glenn
Hi Ozo,

Thanks. Your submission works well.