Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Script to remove spaces and mv a file

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-20-2018   -   Original Discussion by w_s_s
w_s_s's Unix or Linux Image
w_s_s w_s_s is offline
Registered User
 
Join Date: Apr 2018
Last Activity: 20 April 2018, 2:11 PM EDT
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Script to remove spaces and mv a file

I've tried various solutions to move a file name with spaces and nothing seems to work. I need to take a date as input, prepend it to a filename with spaces then remove the spaces and mv the file to the new name.


Code:
#!/bin/ksh
#

if (( $# != 1 ))
then
   echo "Usage: `basename $0` <DATE> "
   echo ""
   exit 1
fi

echo " "
IN_DATE=${1}
IN_FILE="_test with space.txt"
echo "date ${IN_DATE}"

echo " "
oldFILE="$IN_DATE""$IN_FILE"

echo " "
newFILE=$(echo "$oldFILE" | sed 's/ /_/g')
echo "newFILE ${newFILE}"

echo " "
mv "${oldFile}" "${newFILE}"

which produces...


Code:
space_test 20180419

date 20180419

oldfile 20180419_test with space.txt

newFILE 20180419_test_with_space.txt

mv: cannot stat ‘’: No such file or directory

I've also tried using awk to add single or double quotes around the original file name.


Code:
oldFILE="$IN_DATE""$IN_FILE"
echo "oldfile ${oldFILE}"
origFILE=$(echo ${oldFILE} | awk '{print "\042"$0"\042"}')
echo "origfile ${origFILE}"

AND

oldFILE="$IN_DATE""$IN_FILE"
echo "oldfile ${oldFILE}"
origFILE=$(echo ${oldFILE} | awk '{print "\047"$0"\047"}')
echo "origfile ${origFILE}"

the echo shows the single or double quotes around the file name but I still get the same error on the mv.


Code:
oldfile 20180419_test with space.txt
origfile "20180419_test with space.txt"

oldfile 20180419_test with space.txt
origfile '20180419_test with space.txt'

I've also tried escaping the spaces with a \ but so far nothing that I've tried has worked. I get the same error. From the command line the mv works using ' or " or escaping the space with \.

Any suggestions?

Last edited by w_s_s; 04-20-2018 at 12:27 PM.. Reason: snytax
Sponsored Links
    #2  
Old Unix and Linux 04-20-2018   -   Original Discussion by w_s_s
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 27 May 2018, 6:18 PM EDT
Location: Aachen, Germany
Posts: 12,756
Thanks: 417
Thanked 3,923 Times in 3,607 Posts
In your first code sample, origFile is unset, thus empty, and mv doesn't find anything and complains as posted.
A caveat: the single quotes in the mv error message seem to be locale dependent - make sure you use ASCII characters for any input.

And, you might want to learn about shells' "parameter expansion / pattern substitution" to eliminate the sed stuff.
Sponsored Links
    #3  
Old Unix and Linux 04-20-2018   -   Original Discussion by w_s_s
w_s_s's Unix or Linux Image
w_s_s w_s_s is offline
Registered User
 
Join Date: Apr 2018
Last Activity: 20 April 2018, 2:11 PM EDT
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks for the reply.

The {origFILE} on the mv was a cut and paste issue from trying different commands. With the mv command as



Code:
echo " " mv "${oldFile}" "${newFILE}"

I receive


Code:
mv: cannot stat ‘’: No such file or directory

For the space substitution I've also tried tr, rename, and doing it in the mv command (mv "$oldFILE" "${oldFILE// /_}"). I will look where you suggested to see if there's other options I haven't tried yet.

Last edited by w_s_s; 04-20-2018 at 12:25 PM.. Reason: typo
    #4  
Old Unix and Linux 04-20-2018   -   Original Discussion by w_s_s
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 27 May 2018, 6:18 PM EDT
Location: Aachen, Germany
Posts: 12,756
Thanks: 417
Thanked 3,923 Times in 3,607 Posts
Please post the result of


Code:
echo mv "${oldFile}" "${newFILE}"

for debugging purposes. Also, run the code with the -vx options set.
Sponsored Links
    #5  
Old Unix and Linux 04-20-2018   -   Original Discussion by w_s_s
w_s_s's Unix or Linux Image
w_s_s w_s_s is offline
Registered User
 
Join Date: Apr 2018
Last Activity: 20 April 2018, 2:11 PM EDT
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts


Code:
 bash -vx space_test 20180420
#!/bin/ksh
#

if (( $# != 1 ))
then
   echo "Usage: `basename $0` <DATE> "
   echo ""
   exit 1
fi
+ ((  1 != 1  ))

echo " "
+ echo ' '

IN_DATE=${1}
+ IN_DATE=20180420
IN_FILE="_test with space.txt"
+ IN_FILE='_test with space.txt'
echo "date ${IN_DATE}"
+ echo 'date 20180420'
date 20180420

echo " "
+ echo ' '

oldFILE="$IN_DATE""$IN_FILE"
+ oldFILE='20180420_test with space.txt'
echo "oldfile ${oldFILE}"
+ echo 'oldfile 20180420_test with space.txt'
oldfile 20180420_test with space.txt

echo " "
+ echo ' '

newFILE=$(echo "$oldFILE" | sed 's/ /_/g')
++ echo '20180420_test with space.txt'
++ sed 's/ /_/g'
+ newFILE=20180420_test_with_space.txt
echo "newFILE ${newFILE}"
+ echo 'newFILE 20180420_test_with_space.txt'
newFILE 20180420_test_with_space.txt

echo " "
+ echo ' '

mv "${oldFILE}" "${newFILE}"
+ mv '20180420_test with space.txt' 20180420_test_with_space.txt
mv: cannot stat ‘20180420_test with space.txt’: No such file or directory

exit
+ exit

---------- Post updated at 01:37 PM ---------- Previous update was at 01:24 PM ----------

I found the typo. I had not used the -vx options before they helped me see the problem. Thanks!

Last edited by w_s_s; 04-20-2018 at 01:34 PM..
Sponsored Links
    #6  
Old Unix and Linux 04-20-2018   -   Original Discussion by w_s_s
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 27 May 2018, 6:18 PM EDT
Location: Aachen, Germany
Posts: 12,756
Thanks: 417
Thanked 3,923 Times in 3,607 Posts
oldFILE != oldFile
The Following User Says Thank You to RudiC For This Useful Post:
w_s_s (04-20-2018)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to remove spaces between the columns in UNIX script?. karingulanagara Shell Programming and Scripting 2 03-01-2013 12:46 AM
Remove spaces in a file i150371485 Shell Programming and Scripting 5 11-23-2012 02:08 AM
Remove spaces / tabs from variable in script lodhi1978 Shell Programming and Scripting 5 12-09-2011 04:13 AM
remove spaces in between file kanikasharma UNIX for Dummies Questions & Answers 4 10-19-2011 07:22 AM
Shell Script to remove spaces while writing to the file dinesh1985 Shell Programming and Scripting 6 11-20-2009 04:31 AM



All times are GMT -4. The time now is 11:23 PM.