9 Type and Count Lines via Regex Notepad++

Asked

Viewed 548 times

5

First doubt:

How to convert a phone book that is in format:

1188888888
1288888888
3188888888
1187877766

for:

11988888888
12988888888
31988888888
11987877766

via Notepad++ regular expression?

Or maybe if possible in PHP, as I have to loop several files. In case it is possible to do this "batch" in Notepad and someone can explain me xD

Remembering that the DDD can change. The entire list is for cell phones, so you don’t need confirmation to know if it is or isn’t. It would just need regex to move the first 2 houses and add the 9.

I tried to do adventures in PHP Dom but it didn’t work very well, let alone using fopen... I even managed to print the cool result, but I can’t export the final value back to csv. If this is the case, I will do it via Notepad++ even. It is more laborious, but it seems to be a simpler solution.

If anyone knows of an app or easier way to do this...

2nd doubt After the 9 digit has been added, I need to add a semicolon and the line count, which looks like this:

11988888888;1
12988888888;2
31988888888;3
11987877766;4

And of course, the list is huge, so I would need it to be very conventional. If you can do both at the same time with just 1 regex, it would be even nicer :D

PS: Now that I noticed that the last line of all the files is blank. That is, if there are 5000 lines in the file, 4999 has numbers, the last one is not. Soon the count should break only where it has number, not to "break" the file formatting.

Could anyone do that? Thank you :D

MY "SOLUTION" IN PHP (is not functional because it does not save the result, only shows on the screen)

<?php
$row = 1;
$handle = fopen ("Lista (1).csv","r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count ($data);
    for ($c=0; $c < $num; $c++) {
    $datanew = substr_replace($data, "9", 2, 0);
        echo $datanew[$c]. ";$row <br />\n";
        $row++;
    }
}
fclose ($handle);
?>  

But it only prints on the screen. I couldn’t get it to overwrite the file.

  • If you did, create an answer don’t put that code in the question. xD is more or less what I was doing with php

  • I could not because it is not fully functional. It does not save the file. It just shows on the screen =/

  • you can create a new file with the modifications using fopen with w+, to add to the ; and the line number follows the same logic as the substr_replace().

  • Could you show me how to do this? I’m a beginner in php, some things I only learn by seeing xD. I’m taking classes at Cursoemvideo.com with Gustavo Guanabara.

  • In csv, there’s only one phone per line?

  • Exactly, 1 line per phone. Need to convert to 9th digit and still put semicolon and line count in front.

  • You need to check a few more things, such as the "special cases" and also the Nextel numbers.

  • There are no "special cases" or Nextel numbers. Lists are basic numbers, all have DDD at the beginning followed by 8 digits. And they are all mobile. That is, I don’t need the script to do checks, it can be a "dumb" script that puts the 9 in the third house and the end semicolon and the line count. No problem.

Show 3 more comments

1 answer

5


Notepad++/Column mode

With Notepad++ you can solve this problem in three steps.

1 - Add the 9, place the courses in the third character of the first line and activate the Edit>column mode menu or (ALT+C), in the dialog box type the 9 and of ok.

2 - Repeat step 1, but this time leave the cursor at the end of the line, instead of the 9 add a ;.

3 - Access the column mode again leave the cursor after the ;, in the dialog box mark the radio number to insert in initial number place 1 which will be the number of the first line and let increse by in 1, finally of ok.

inserir a descrição da imagem aqui

PHP

With php it doesn’t change the logic much as the string looks well formatted, it can be done based on size.

substr_replace() is responsible for adding first the 9 in position 2 of the manipulated line. Another addition occurs in the character 12(end of string) where the ;, followed by the line number($linha_atual) e de um quebra de linha(PHP_EOL), tudo isso é concatenado em$archivalque é o conteúdo do no arquivo, ele é criado comfile_puts_contents()`.

<?php

    $csvs = glob('*.csv');

    foreach($csvs as $index => $nome){
        $linha_atual = 1;
        $arquivo_novo = "";

        $handle = fopen ($nome, 'r');
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $linha_modificada = substr_replace($data[0], '9', 2, 0);
            $arquivo_novo .= substr_replace($linha_modificada, ';'. $linha_atual 
                             .PHP_EOL , '12');    
            $linha_atual++;
        }
        fclose ($handle);
        file_put_contents('novo'. $index .'.csv', $arquivo_novo);
    }
  • I already put the images

  • Wow, awesome! ?

  • Automate? @diego, I think you can do it with php, then edit

  • That’s it! Automate it. Because each file has many rows of numbers and are multiple files in multiple folders. If I could run it by briefcase, it would be sensational. I tried to write a Macro inside Notepad++ itself but it doesn’t seem to repeat the things that are done by ALT+C (Column Mode). If you can do everything via PHP even better. The files are named as follows: List (1). csv List (2). csv List (3).csv..... Remember that I do not need to print on the screen. I need to save in the same file. That is, open, edit and save. What I really need is the modified file.

  • Take a look at my edited question @rray . I put my brief "solution", but it is not functional as it does not overwrite the csv file.

  • Perfect, it worked. Thanks =D. Can you tell me if you could make a "loop" in the name of the file to open the subsequent one? Lists are numbered with parentheses between the number. Getting: List (1). csv and the number 1 changes according to the number in the list.

  • @Diego from yes, you can use that code: $arquivos = glob('*.csv'); you will need a foreach before the while at the time of opening the file will no longer be the fixed name, will be the variable name in foreach. Do not forget to start $linha_atual and $arquivo_novo every new file.

  • Could you edit the script? I tried it with 'for ($i = 1; $i <= 100; $i++' and then put the file name '"List (". $i.". csv"' but it didn’t work :( Foreach still don’t know how to use.

  • @Diego soon edits again.

  • I got it via FOR, but with each new file created it copies the previous one. Then the next file gets even bigger. As it would be in FOREACH?

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.