LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-10-2014, 05:49 PM   #1
pople
LQ Newbie
 
Registered: Nov 2013
Distribution: Gentoo
Posts: 26

Rep: Reputation: Disabled
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.
 
Old 03-10-2014, 06:31 PM   #2
pople
LQ Newbie
 
Registered: Nov 2013
Distribution: Gentoo
Posts: 26

Original Poster
Rep: Reputation: Disabled
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
 
Old 03-10-2014, 09:37 PM   #3
pople
LQ Newbie
 
Registered: Nov 2013
Distribution: Gentoo
Posts: 26

Original Poster
Rep: Reputation: Disabled
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
 
Old 03-10-2014, 10:07 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,000

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
If you wish to place a string after a variable name, simply enclose the variable in {}:
Code:
mv "$PWD/$file" "$PWD/${users}.pdf"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing strings in linux raviraj2018 Linux - Software 3 07-30-2009 03:04 AM
problem with comparing strings in php Yoyo302 Programming 5 06-30-2009 04:16 AM
c++ - if/else problem comparing strings babag Programming 14 05-19-2008 11:32 PM
comparing 2 strings in shell script dhanabalanb Programming 3 08-01-2007 01:17 PM
Error comparing strings and acting on the comparison WindowsBurner Programming 4 10-21-2004 12:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration