LinuxQuestions.org
Register a domain and help support LQ
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, 12:11 PM   #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, 12:18 PM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 15,073

Rep: Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712
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, 12:49 PM   #3
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 1,769

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
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, 01: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, 01:29 PM   #5
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 15,073

Rep: Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712
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, 02: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, 09:06 PM   #7
BowCatShot
LQ Newbie
 
Registered: Aug 2014
Posts: 13

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 10:04 PM.
 
Old 08-18-2014, 02:55 PM   #8
mfarch99
LQ Newbie
 
Registered: Mar 2012
Posts: 20

Original Poster
Rep: Reputation: Disabled
Thanks, will try

thanks
 
Old 08-18-2014, 03:29 PM   #9
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,197

Rep: Reputation: 642Reputation: 642Reputation: 642Reputation: 642Reputation: 642Reputation: 642
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 03:36 PM.
 
Old 08-22-2014, 10: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, 03: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


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


All times are GMT -5. The time now is 10:13 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration