0

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

file1.failed:

FHEAD|4525|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 all the text between THEAD| and |2 to a output file. im trying the following and it works only if i have only one file in the directory.

sed -n 's:.*THEAD|\(.*\)|2.*:\1:p' <*.failed >transactions.log

The output is: transactions.log:

150001021
150001022

Now how can i do the same but for multiple files?(undetermined number of files).

Also it is possible to add the filename in every line of the output file as follows?

expected output:

file1.failed,150001021
file1.failed,150001022
file2.failed,150001023
file2.failed,150001024
file2.failed,150001025
file2.failed,150001026
file3.failed,150001027
file3.failed,150001028

Thanks in Advance,

0

1 Answer 1

1

(tested with gawk and mawk)

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

cat transactions.log 
file1.failed,150001021
file1.failed,150001022
file2.failed,150001023
file2.failed,150001024
file3.failed,150001025
file3.failed,150001026
3
  • Hello Master of AWK, It's exactly that output that i expect. I will now try to understand it fully. Best regards, Commented Jan 25, 2017 at 23:48
  • @CarlosPinho I cheated a bit - it doesn't exactly extract "text between THEAD| and |2", instead it extracts the second pipe-delimited field when the first field matches THEAD. If you really need to check that the following field starts with 2, or if THEAD can occur elsewhere in the line, the solution will need adjustment -lmk Commented Jan 25, 2017 at 23:53
  • In this case, the files are created automatically so THEAD it only occurs once per line, actually your solution is even better that the initial requirement. Note that the 2 field stands for the 2 in 2017(current year) it means that your solution will be good even in 3000 year and so on :) Commented Jan 26, 2017 at 0:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .