mule-studiodataweavemulesoftanypoint-studiomule4

Extract lines from input text file to text file with some transformation with Mule 4


I have a requirement where I need to read text file and extract some data and send the extracted to other system for which am unable to do it.

Input file:

1BoraBora Island                                                                                                                                                           
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
3BR          209078 BoraBora              6798989                                                                                                99999
1 BR  67854 JAIHIND  789                                                                                                 000Y247          9898983
2 BR  CR9   BoraBora 123                                                QK                                          J12Y64              00010520
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Output should be:

 
1BoraBora Island                                                                                                                                                          
0000000000000000000000
1 BR  67854 JAIHIND  789                                                                                                 000Y247          9898983
2 BR  CR9   BoraBora 123                                                QK                                          J12Y64              00010520

Need to extract only row having "BR" in it at 3th letter. Please guide me how to achieve this in text format only.


Solution

  • Assuming that the input is `text/plain'. Using a DataWeave script and the subscript() function you can extract a given position from the input:

    %dw 2.0
    import * from dw::core::Strings
    output text/plain
    var lines=payload splitBy "\n" // separate text into an array of lines
    ---
    lines[0] ++"\n" ++ lines[1] ++"\n" 
    ++ (lines[2 to -1] // use the range selector to get the remaining lines
        filter (substring($,2,4)=="BR") // filter lines that have "BR" at the right position
        reduce ($$++"\n"++$) // concatenate the remaining lines again into a single text file
    )
    

    Output:

    1BoraBora Island                                                                                                                                                           
    00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    1 BR  67854 JAIHIND  789                                                                                                 000Y247          9898983
    2 BR  CR9   BoraBora 123                                                QK                                          J12Y64              00010520