Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture results of regex and replace patterns in bash

Tags:

regex

bash

Bash scripting does my head in. I have searched for regex assignment, but not really finding answers I understand.

I have files in a directory. I need to loop through the files and check if they fit certain criteria. File names under a certain sequence need to have their sequence increased. Those over a certain sequence need to trigger an alert.

I have pseudo code and need help turning it into correct bash syntax:

#!/bin/sh

function check_file()
{
    # example file name "LOG_20101031144515_001.csv"
    filename=$1

    # attempt to get the sequence (ex. 001) part of file

    # if sequence is greater than 003, then raise alert

    # else change file name to next sequence (ex. LOG_20101031144515_002.csv)
}

for i in `ls -Ar`; do check_file $i; done;

If PHP were an option, I could do the following:

function check_file($file){
    //example file 'LOG_20101031144515_001.csv';

    $parts = explode('.',$file);
    preg_match('/\d{3}$/', $parts[0], $matches);
    if ($matches){
        $sequence = $matches[0];
        $seq      = intval($sequence);

        if ($seq > 3){
            // do some code to fire alert (ex. email webmaster)
        }
        else{
            // rename file with new sequence
            $new_seq  = sprintf('%03d', ++$seq);
            $new_file = str_replace("_$sequence", "_$new_seq", $file);
            rename($file, $new_file);
        }
    }
}

So long story short, I'm hoping someone can help port the PHP check_file function to the bash equivalent.

Thank you

like image 243
Jordan Avatar asked Dec 06 '25 20:12

Jordan


2 Answers

First of all, your question is tagged [bash], but your shebang is #!/bin/sh. I'm going to assume Bash.

#!/bin/bash
function check_file()
{
    # example file name "LOG_20101031144515_001.csv"
    filename=$1

    # attempt to get the sequence (ex. 001) part of file

    seq=${filename%.csv}
    seq=${seq##*_}

    # if sequence is greater than 003, then raise alert

    if (( 10#$seq > 3 ))
    then
        echo "Alert!"
    else
        # else change file name to next sequence (ex. LOG_20101031144515_002.csv)
        printf -v newseq "%03d" $((seq + 1))
        echo "${filename/$seq/$newseq}" # or you could set a variable or do a mv
    fi
}
like image 120
Dennis Williamson Avatar answered Dec 08 '25 12:12

Dennis Williamson


PHP IS an option. If you master PHP, you can run it from shell. Run

php myfile.php

and get the output right into console. If the PHP file is executable and begins with

#!/path/to/php/executable

then you can run

./myfile.php

I'm no big expert in bash programming, but in order to obtain the list of files that match a certain patter you can use the command

ls -l | grep "pattern_unquoted"

I suggest you to go for the PHP ;-)

like image 25
usr-local-ΕΨΗΕΛΩΝ Avatar answered Dec 08 '25 10:12

usr-local-ΕΨΗΕΛΩΝ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!