0

I have some directory with multiple files with the extention .failed This number of files can change every day. This files have the following format:

file1_string2_1.failed:

FHEAD|string2|20170109000000|20170125024831
THEAD|150001021|20170109121206||
TDETL|4000785067||1|EA|||RETURN|||N
TTAIL|1
THEAD|150001022|20170109012801||
TDETL|4000804525||1|EA|||RETURN|||N
TTAIL|1
FTAIL|6

I need to extract the next 2 strings after THEAD| to a output file separated by comma. I also need the filename and the string2 extracted to this output file. Note that string2 can be used from the filename or from the FHEAD tag.

expected output:

file1_string2_1.failed,string2,150001021,20170109121206
file1_string2_1.failed,string2,150001022,20170109012801
file2_string2_1.failed,string2,150001023,20170109100904
file2_string2_2.failed,string2,150001024,20170109031206
file2_string2_3.failed,string2,150001025,20170109081207
file3_string2_1.failed,string2,150001026,20170109141203
file3_string2_2.failed,string2,150001027,20170109121208
file4_string2_1.failed,string2,150001028,20170109171206

For now i have the following command:

awk -F'|' '$1 == "THEAD" {print FILENAME, $2}' OFS=, *.failed > failed_transactions.out

The output that im getting is:

file1_string2_1.failed,150001021
file1_string2_1.failed,150001022
file2_string2_1.failed,150001023
...
0

You can split FILENAME into underscore-separated fields and place the result in an array

split(FILENAME,a,"_")

After that, it's just a matter of adding the required elements to the print statement

print FILENAME, a[2], $2, $3

So

awk -F'|' '$1 == "THEAD" {split(FILENAME,a,"_"); print FILENAME, a[2], $2, $3}' OFS=, *.failed > failed_transactions.out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.