Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Renaming Files Based on a Specific Pattern

Status
Not open for further replies.

dayankoven

Programmer
Oct 28, 2002
17
MY
Hi there fellow Experts,

Was hoping someone could shed some light as to how i can achieve the below :-

I have a list of files at the specific directory for eg :-
BWCRUCNT1A
BWCRUCNT1B
BWCRUCNT1C

I need to create a UNIX .sh script that will copy the file over from this directory to a another working directory.
The problem is when copied over the files must not have the alphabet extension at the back. So, for this case the file must be copied as BWCRUCNT1. Another requirement is that i must pass in the original filename into the script. So, in short, the script must accept one parameter(original filename) and then based on this parameter it must then modify the parameter(filename) to remove the last character at the end and copy the file over to the working directory.

Appreciate all the help and thanks in advance.

 
Try something like this:
Code:
cd /path/to/destdir
input=$1
output=`basename $1 | sed 's!.$!!'`
cp $input $output`[\code]

Hope This Help
PH.
 
PH,

Thanks so much. It works perfectly. Would really appreciate if you could spare a few moments to explain what the script is actually doing especially the following :-

output=`basename $1 | sed 's!.$!!'`

Thanks again.
 
I guess that you maybe interested in command 'sed' doing.
PH using '!' as a delimitor. Usually we use '/'. Anyway, both are work.

I seperate it in few of parts.
Part 1:
s -- stand for substitute
Part 2:
!.$! -- Pattern Field. The regular expression .$ matches last character.
Part 3:
!! -- Replacement Field. Because it is nothing between the delimiters. So it is null.

tikual
 
Using parameter substitution....

output=${1%?}

...would delete the last character of $1 and assign to $output
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top