Jump to content

php combine identical lines in text file


erisoov

Recommended Posts

I have the following code

$fp = fopen('/output/report_sort.txt', 'w');
if(!$fp)
    die('Could not create / open text file for writing.');
if(fwrite($fp, $txt1) === false)
    die('Could not write to text file.');

    // Print the totals for each category
$data = file('/output/report_sort.txt', FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
usort($data, fn($a, $b)=>substr($a,11,2)<=>substr($b,11,2));              // sort by code number
$codes = [];
foreach ($data as $d) {
    $p = strpos($d, ')');
    $k = substr($d, 11, $p-10);
    if (!isset($codes[$k])) {
        $codes[$k] = 1;
    } else {
        $codes[$k]++;
    }
}
//echo (join("<br>", $data));
echo ("<font color='blue'><b><h3>Total clicks</h3></b></font>");
foreach ($codes as $code => $total)  {
    printf("%s <font color='red'><b>Total clicks = %d<br></b></font>", $code, $total); 
}
echo "<br />";

which from this data

05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Spain Cartagena Murcia
05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Spain Nerja Malaga
05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Cedar Springs Michigan
05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Spain Madrid Madrid
06/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Edmore Michigan
06/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Edmore Michigan
06/10/2023 03 Resist the Curse (Resiste la Maldición) 11-12-2022.mp3 France  
08/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Edmore Michigan
08/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Edmore Michigan
08/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
08/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 23 The Wedding Part 1 (La Boda) 30-7-2023.mp3 Ireland Dublin Leinster

22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 Spain Nerja Malaga
22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 Spain Cartagena Murcia

outputs the totals for each category as follows

03 Resist the Curse (Resiste la Maldición) Total clicks = 2
04 Feast of Trumpets 2023 (Fiesta de las Trompetas) Total clicks = 19
05 Day of Atonement 2023 (Día de la Expiación) Total clicks = 32
06 Feast of Tabernacles 2023 (Fiesta de los Tabernáculos) Total clicks = 4
07 The Eighth Day 2023 (El Octavo Día) Total clicks = 17
19 The Bread of Life (El Pan de la Vida) Total clicks = 20
20 Be With Me Where I Am (Está Conmigo Donde Estoy) Total clicks = 4
20 The Legacy of Jesus (El Legado de Jesús) Total clicks = 2
23 The Wedding Part 1 (La Boda) Total clicks = 1

what I would like is to combine lines which are duplicated to show 1 line as 1 click as follows

05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Spain Cartagena Murcia
05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Spain Nerja Malaga
05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Cedar Springs Michigan
05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Spain Madrid Madrid
06/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Edmore Michigan
06/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Edmore Michigan
06/10/2023 03 Resist the Curse (Resiste la Maldición) 11-12-2022.mp3 France  
08/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Edmore Michigan
08/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Edmore Michigan
08/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
08/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster

To be shown in the totals as 
03 Resist the Curse (Resiste la Maldición) Total clicks = 1
07 The Eighth Day 2023 (El Octavo Día) Total clicks = 1

the same for all the other categories

 

Link to comment
Share on other sites

I don't follow, partly because I don't understand what you mean by "combine" and partly because the code and samples you've posted don't fit together.

You already have something that can count the number of times a "code number" is used. What does that code not do which you need?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.