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|021234567 8|email@do main.com|
TEXT
||US|123457|E567|021234428 6||
TEXT
||AU|123456|E098|021234712 9||
TEXT
||UK|123458|E123|021234753 2|email@do main.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_0 04)
Thanks in advance,
Glenn
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|021234567
TEXT
||US|123457|E567|021234428
TEXT
||AU|123456|E098|021234712
TEXT
||UK|123458|E123|021234753
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,
Thanks in advance,
Glenn
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 !
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
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
ASKER
Hi Ozo,
Thanks. Your submission works well.
Thanks. Your submission works well.
please could you explain the solution you gave. i am also facing same problem.