[SOLVED] Need to remove fist part of files and ensure the new name starts with SCAL
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Need to remove fist part of files and ensure the new name starts with SCAL
I'm currently trying to rename a series of files I have downloaded using mget. The files are coming from a mainframe so they have dataset qualification names. When I perform the mget the files come down with single quotes surrounding them. I'm running the following script to take those quotes off and was trying to use the same process to remove part of the names. Problem is the part I identify to remove goes away, but so does the first 3 characters of the last name.
The file names are 'ADCDMST.ENG.SCAL147D' 'ADCDMST.ENG.SCAL14PE', and 'ADCDMST.ENG.SCAL14WE'.
I want these files to end up with SCAL147, SCAL14PE, SCAL14WE.
Here is the code I'm currently using to rename.
for filename in `ls *SCAL*`
do
echo $filename
new_filename=`echo $filename| tr "'" " "`
new_filename=`echo $new_filename| tr -d "ADCDMST.ENG."`
echo $new_filename
# mv $filename $new_filename
done
echo "Files Received"
Using this code the files always end up like L147, L14PE, L14WE:
./name.sh
'ADCDMST.ENG.SCAL147D'
L147
'ADCDMST.ENG.SCAL14PE'
L14P
'ADCDMST.ENG.SCAL14WE'
L14W
Files Received
I'm currently trying to rename a series of files I have downloaded using mget. The files are coming from a mainframe so they have dataset qualification names. When I perform the mget the files come down with single quotes surrounding them. I'm running the following script to take those quotes off and was trying to use the same process to remove part of the names. Problem is the part I identify to remove goes away, but so does the first 3 characters of the last name.
The file names are 'ADCDMST.ENG.SCAL147D' 'ADCDMST.ENG.SCAL14PE', and 'ADCDMST.ENG.SCAL14WE'.
I want these files to end up with SCAL147, SCAL14PE, SCAL14WE.
Here is the code I'm currently using to rename.
for filename in `ls *SCAL*`
do
echo $filename
new_filename=`echo $filename| tr "'" " "`
new_filename=`echo $new_filename| tr -d "ADCDMST.ENG."`
echo $new_filename
# mv $filename $new_filename
done
echo "Files Received"
Using this code the files always end up like L147, L14PE, L14WE:
./name.sh
'ADCDMST.ENG.SCAL147D'
L147
'ADCDMST.ENG.SCAL14PE'
L14P
'ADCDMST.ENG.SCAL14WE'
L14W
Files Received
...has a little shell one-liner you could use to do exactly what you're after. Modify the sed parameters, so instead of removing parens, remove the "ADCDMST.ENG."
Note: don't use an extra "ls" in bash scripts. Bash already has good pathname expansion that doesn't suffer the quirks that ls adds. For example, ls is an alias for many users, so you should at least always use /bin/ls to get the real one.
Tried addig the following to the script but still receive same results. Truncates the last part of the file name.
shopt -s nullglob
for filename in *SCAL*
Also tried to use the one liner and that did not work either. I replaced the code with the following and receive an the error message below; for i in *\(*\)*; do j=`echo $i |sed -e 's/["ADCDMST.ENG"]//g'`; mv "$i" "$j"; done
Error message:
mv: cannot stat `*(*)*': No such file or directory
Also tried to use the one liner and that did not work either. I replaced the code with the following and receive an the error message below; for i in *\(*\)*; do j=`echo $i |sed -e 's/["ADCDMST.ENG"]//g'`; mv "$i" "$j"; done
Error message:
mv: cannot stat `*(*)*': No such file or directory
Right, because you didn't modify what was getting found, and have an incorrect sed. The sed doesn't need the brackets or the double-quotes...brackets indicate a regular expression, and the double-quotes aren't escaped. The "for i in..." needs to be the *SCAL*, since that is the pattern it will look for..right now, it's looking for file names with parens in them.
Re-read the other thread, and look at the explanation of what that one-liner is doing...you will have to modify it to make it do what YOU want.
for mainframe i sometimes log in via ftp (since these dinosaur machines dont have ssh/scp).
if i do cd .. then get dsn.name without the single-quotes the downloaded file wont have single-quotes.
Actually, this can easily be done right within bash, without tr/sed/awk or whatnot:
1. The "classic" way, if you have an older bash:
Code:
#!/bin/bash
DIR="$1"
test -z "$DIR" && DIR="."
for i in "$DIR"/*SCAL* ; do
NEWNAME=${i##*.} # Strip everything up to last dot
NEWNAME=${NEWNAME%\'} # Strip trailing '
mv "$i" "$DIR/$NEWNAME"
done
2. Or with regular expressions in non-ancient bash:
Code:
#!/bin/bash
DIR="$1"
test -z "$DIR" && DIR="."
REGEXP=".*(SCAL.*)'"
for i in "$DIR"/*SCAL* ; do
if [[ "$i" =~ $REGEXP ]] ; then
mv "$i" "$DIR/${BASH_REMATCH[1]}"
else
echo "$i doesn't match"
fi
done
Actually, this can easily be done right within bash, without tr/sed/awk or whatnot:
1. The "classic" way, if you have an older bash:
Code:
#!/bin/bash
DIR="$1"
test -z "$DIR" && DIR="."
for i in "$DIR"/*SCAL* ; do
NEWNAME=${i##*.} # Strip everything up to last dot
NEWNAME=${NEWNAME%\'} # Strip trailing '
mv "$i" "$DIR/$NEWNAME"
done
2. Or with regular expressions in non-ancient bash:
Code:
#!/bin/bash
DIR="$1"
test -z "$DIR" && DIR="."
REGEXP=".*(SCAL.*)'"
for i in "$DIR"/*SCAL* ; do
if [[ "$i" =~ $REGEXP ]] ; then
mv "$i" "$DIR/${BASH_REMATCH[1]}"
else
echo "$i doesn't match"
fi
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.