LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 08-15-2014, 11:11 AM   #1
mfarch99
LQ Newbie
 
Registered: Mar 2012
Posts: 20

Rep: Reputation: Disabled
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


Any assistance would be appreciated. Thanks.
 
Old 08-15-2014, 11:18 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by mfarch99 View Post
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


Any assistance would be appreciated. Thanks.
This thread:
http://www.linuxquestions.org/questi...me-4175513716/

...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."
 
Old 08-15-2014, 11:49 AM   #3
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
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.

Code:
shopt -s nullglob
for  filename in *SCAL*
 
Old 08-15-2014, 12:03 PM   #4
mfarch99
LQ Newbie
 
Registered: Mar 2012
Posts: 20

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-15-2014, 12:29 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by mfarch99 View Post
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.
 
Old 08-15-2014, 01:04 PM   #6
mfarch99
LQ Newbie
 
Registered: Mar 2012
Posts: 20

Original Poster
Rep: Reputation: Disabled
Thanks TB0ne. Looks like I have it.. Sorry for the confusion...
 
Old 08-15-2014, 08:06 PM   #7
BowCatShot
LQ Newbie
 
Registered: Aug 2014
Posts: 15

Rep: Reputation: Disabled
Taking

'ADCDMST.ENG.SCAL147D'

for an example.

Once you've removed the single quotes so that fn = ADCDMST.ENG.SCAL147D, try this

newfn=$(echo $fn | cut -d'.' -f 3)

mv $newfn $fn

Last edited by BowCatShot; 08-17-2014 at 09:04 PM.
 
Old 08-18-2014, 01:55 PM   #8
mfarch99
LQ Newbie
 
Registered: Mar 2012
Posts: 20

Original Poster
Rep: Reputation: Disabled
Thanks, will try

thanks
 
Old 08-18-2014, 02:29 PM   #9
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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.

alternate:
Code:
[schneidz@hyper ~]$ echo first,second,third,fourth | awk -F , '{print $3}'
third

Last edited by schneidz; 08-18-2014 at 02:36 PM.
 
Old 08-22-2014, 09:11 AM   #10
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 297

Rep: Reputation: 49
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
 
Old 08-25-2014, 02:32 PM   #11
mfarch99
LQ Newbie
 
Registered: Mar 2012
Posts: 20

Original Poster
Rep: Reputation: Disabled
That works great Thanks....

This works great. Thanks..


Quote:
Originally Posted by rupertwh View Post
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
 
  


Reply



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
[SOLVED] bash - remove reboot history when new term starts up fedoraDrew Linux - Newbie 2 07-10-2013 02:02 PM
How can I ensure that files created inherit both user and group of directory vfclists Linux - Software 1 11-21-2009 07:37 PM
How to ensure that a script start before kde starts? adityavpratap Slackware 3 06-03-2007 03:42 AM
When configuring files, how do I ensure they get to the right directory? fortunekiller Slackware 7 06-15-2006 10:58 PM
kmail starts automatically upon login - how to remove? Yalla-One Slackware 18 02-13-2006 04:15 PM

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

All times are GMT -5. The time now is 05:54 PM.

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