LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash Comparing similar strings (https://www.linuxquestions.org/questions/programming-9/bash-comparing-similar-strings-4175497732/)

pople 03-10-2014 05:49 PM

Bash Comparing similar strings
 
I am writing a bash script to rename complex files names to somehting simpler. Is there a way in bash to find part of a string that is similiar as opposed to exactly the same? For example: test1 and random_complex_file_test1.txt would match and the file could then be renamed test1.

pople 03-10-2014 06:31 PM

This is where I'm at:

Code:

#! /bin/bash

echo -n "Username file?: "
read usersfile
echo $usersfile
echo $PWD/$usersfile

while read users
do 
    for file in *.pdf
do
        if [[ $users =~ $file ]]
        then
            cp $file $users".pdf"
        fi
    done
done < $PWD/$usersfile


pople 03-10-2014 09:37 PM

This kind of works will add extra features later. Here it is if anyone else wantes to try this:

Code:

#! /bin/bash

echo -n "Username file?: "
read usersfile
echo "Using the user list file: $PWD/$usersfile"
echo "Converting"
sleep 1
while read users
do 
    for file in *.pdf
    do
        if [[ $file =~ $users ]]
        then
            echo $users
            mv "$PWD/$file" "$PWD/$users"".pdf"
        fi
    done
done < $PWD/$usersfile


grail 03-10-2014 10:07 PM

If you wish to place a string after a variable name, simply enclose the variable in {}:
Code:

mv "$PWD/$file" "$PWD/${users}.pdf"


All times are GMT -5. The time now is 02:35 PM.