Menu

Help w/ splitting plain text file with regexp

2009-03-04
2012-11-13
  • Andreas Carlbom

    Andreas Carlbom - 2009-03-04

    Hello there. I have a number of plain text files in the following format

    Plain text file name: Category

    Every line is in the following format:

    Name-Country

    Now, what i want to do is save all the plain text files by country, so for example the lines

    Amber Jefferson-Sweden
    Thomas Anderson-Sweden
    Connor Macleod-Sweden

    Would all be saved in Sweden.txt, with the -Sweden part omitted.

    I'm really a novice when it comes to stuff like this, and any help is appreciated.

    //DBG

     
    • Fool4UAnyway

      Fool4UAnyway - 2009-03-04

      If the number of countries is small (and the number of files as well) you could use Notepad++ and a regular expression to order your information and then store it manually in files per country.

      In either the normal Ctrl+F/R or advanced Ctrl+R find and replace dialog, enter the following expressions:

      Find field:
      ^(.*)-([^\-]+)$

      Replace field:
      \2-\1

      This will reorder the contents of each line into "Country"-"name". The name itself may even contain - characters: only the last - is seen as separator before the country name.

      When you have done this, you could sort all lines alphabetically: the information will be grouped by country.

      Then you could manually split the list(s) into documents per country.

      You can remove the country name and - character by using column block selection.

      Select all lines and move the cursor to the - character on the first or last line.
      Keep the Shift button down while also pressing Alt and Cursor Right.
      Press Delete to remove the select column, leaving only the names.

       
    • Fabio

      Fabio - 2009-03-04

      Since you have to split files, I don't think N++ is the right tool. I'd use something like AWK.
      What you have to do is:
      1) Look for the "-" charachter in every line, using the match($0, /-/) function. Its position in automatically saved into the RSTART variable
      2) Read the rest of the line, starting from there, and save that into a string: that's the country name
      3) Read from the beginning to the position previous to "-", and save that into another string: that's the person's name
      4) Print the person's name into a file that has the country name.

      Here it is:
      {
          if (match($0, /-/)) {
              countryname = substr($0, RSTART + 1);
              person_name = substr($0, 1, RSTART - 1);
              print person_name >> countryname".txt";
          }
      }

      Save this code into a file called countryname.awk and run the command "awk -f countryname.awk listfile", where listfile is, well,  the name of your list file :-)
      AWK comes with many Linux distributions, and is also available for Windows.

      Hope this helps you!

       
    • Andreas Carlbom

      Andreas Carlbom - 2009-03-04

      I couldn't quite wrap my head around AWK, so i ended up doing it in C#. Thanks for the help though!

      //DBG