none
How do you extract data from a txt file with powershell

    Question

  • Hi i need help. I need to extract data from a txt file whenever the string contains A300 or A400 i need to extract that entire row and the next row to a separate file the file name is RS_Boo_5_18_2010.txt:

    This is a sample of the data:

             6          0 20681                102        Ms   N   SWAYI                          PO BOX 10                      DUTYWA                         DUTYWA                                                        5000      4152.83       522.86       231.86 3177                              300.00 31/08/2010                           0860440804                                         Barnetts                                                   0.00         0.00 ZA                      0201000064520681000000000000000060660183 CHEQUE                         LAEST NATIONoL BANK  240321     31750087648          18/04/2011 0345386052    
    20681                                  02      4152.83       291.00       231.86       522.86 28/02/2011 01/05/2011
             6          0 20691                101        Ms   N   HEISI                          P O BOX 474                    MOUNT FLETCHER                                                                               4770      5855.41       505.68        55.68 3097                              450.00 21/02/2011 03734125                  0860110804                                         Baretts                                                   0.00         0.00 ZA                      0201000060520691000000000000000050160184 Unknown                        Second NATIONAL BANK             55690035630          18/04/2011 0317884052    
    20691                                  07      5855.41       450.00        55.68       505.68 17/04/2011 01/05/2011
             6          0 20693                A400       Ms   L   KHOSA                          P O BOX 7417                   XIMHUNGWE                                                                                    1281      4485.45      3740.99      3443.99 3094                              300.00 10/03/2011                           0860110804                                         BaBluetts                                                   0.00         0.00 ZA                      0204000060520693000000000000000080160185 CHEQUE                         THIRD NATIONAL BANK             62050409373          18/04/2011 0315684052    
    20693                                  00      4485.45       297.00      3443.99      3740.99 04/03/2011 01/05/2011
             6          0 20711                101        Mrs  VM  NYALUNGU                       STAND NO.163 GA DUNA BALOYI KA STRATA SA NDUNA BALOYI NEXT TO MARIA BALOI TSHAMAHA                                          0600     16291.41      1260.49       586.49 3681                              800.00 05/04/2011 015 491 3156              0860117804                                         Bahgetts                                                   0.00         0.00 ZA                      0201000060520711000000000000000056160186 Unknown                        Fiveth NATIONAL BANK             53517883247          18/04/2011 0315384052    
    20711                                  07     16291.41       674.00       586.49      1260.49 05/04/2011 01/05/2011
             6          0 20757                102        Ms   SN  MKHWANAZI                      P O BOX 244                    MBONAMBI                                                                                     3915      1479.60       399.67       162.67 3223                              237.00 05/03/2011 0356501117                0845110804                                         Bratnetts                                                   0.00         0.00 ZA                      0201000060520757000000000000000060160187                                FIRST NATIONAL BANK             62239302679          18/04/2011 0315684052    
    20457                                  00      1479.60       237.00       162.67       399.67 05/03/2011 01/05/2011
             6          0 20758                102        Ms   ZZ  MFEKAYI                        P O BOX 1081                   MTUBATUBA                                                                                    3935      1506.62       481.61       219.61 3223                                0.00            0355501117                0860156804                                         Barnetts                                                   0.00         0.00 ZA                      0201000060520758000000000000000060160188                                FIRST NATIONAL BANK             67819302679          18/04/2011 0315384052    
    20658                                  07      1506.62       262.00       219.61       481.61 28/02/2011 01/05/2011

    Saturday, May 07, 2011 5:07 AM

Answers

  • Here's one way, using the  -context option of select-string:

     

    $filename = "RS_Boo_5_18_2010.txt"
    $output_file = "extract_$filename"

    $extract = @()
    select-string -path $filename -pattern "A300|A400" -context 0,1 |
        foreach-object {
        $extract += $_.line
        $extract += $_.context.postcontext
        }

    $extract | out-file $output_file


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Saturday, May 07, 2011 12:24 PM
  • $contentArr = get-contents d:\RS_Boo_5_18_2010.txt

    $newArr = @()

    for($i =0;$i -lt $contentArr.count; $i++)

    {

    if(($contentArr[$i] .contains("A300")) -or ($contentArr[$i] .contains("A400")))

    {

    $newArr += $contentArr[$i]

    if( ($i + 1) -ne $content.count) //Not at last row

    {

    $i++;

    $newArr += $contentArr[$i]

    }

    }

    $newArr | out-file d:\newFile.txt

    Saturday, May 07, 2011 6:39 AM

All replies

  • $contentArr = get-contents d:\RS_Boo_5_18_2010.txt

    $newArr = @()

    for($i =0;$i -lt $contentArr.count; $i++)

    {

    if(($contentArr[$i] .contains("A300")) -or ($contentArr[$i] .contains("A400")))

    {

    $newArr += $contentArr[$i]

    if( ($i + 1) -ne $content.count) //Not at last row

    {

    $i++;

    $newArr += $contentArr[$i]

    }

    }

    $newArr | out-file d:\newFile.txt

    Saturday, May 07, 2011 6:39 AM
  • Here's one way, using the  -context option of select-string:

     

    $filename = "RS_Boo_5_18_2010.txt"
    $output_file = "extract_$filename"

    $extract = @()
    select-string -path $filename -pattern "A300|A400" -context 0,1 |
        foreach-object {
        $extract += $_.line
        $extract += $_.context.postcontext
        }

    $extract | out-file $output_file


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Saturday, May 07, 2011 12:24 PM
  • What about this:

     

    Get-Content c:sample.txt | select-string "A300"

     

    Hopefully this helps.


    Up The Irons!
    Sunday, May 08, 2011 1:22 PM