Issue with File Renaming


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue with File Renaming
# 1  
Old 09-16-2009
Issue with File Renaming

I am trying to perform some validations on the source files. the script has file name pattern as an argument.The Input file has Single quotes in the filename . I want to remove those single quotes and split the file into 2 parts. below is my code

Code:
FILE_NAME=$1
if [ -f $FILE_NAME ]
then 
    echo "\n Files are available to process \n"
else 
    echo "\n File does not exist: Process is Terminating \n"
    exit 1
fi

for FILE_NAME in $FILE_NAME
do
eval mv -f '$FILE_NAME' $File_NAME
sed -ne '2,1p' $File_NAME > Hdr_$File_NAME
sed -ne '3,$p' $File_NAME > Dtl_$File_NAME
done

The problem is the split Files get renamed with zero bytes and the Single quotes still exist but the original files get renamed with data existing.

Below is the example of scenario

I am receiving multiple files in the below format

'abc.def.ghi.jkl'
'abc.def.xty.xyz'
'abc.def.rew.ter'

I want to split and rename each of these files with out the single quotes as below.

Hdr_abc.def.ghi.jkl
Hdr_abc.def.xty.xyz
Hdr_abc.def.rew.ter

Dtl_abc.def.ghi.jkl
Dtl_abc.def.xty.xyz
Dtl_abc.def.rew.ter

But instead the output is with Zero bytes for each file.


Hdr_'abc.def.ghi.jkl'
Hdr_'abc.def.xty.xyz'
Hdr_'abc.def.rew.ter'

Dtl_'abc.def.ghi.jkl'
Dtl_'abc.def.xty.xyz'
Dtl_'abc.def.rew.ter'

but the original files are Renamed and it has all the data.

abc.def.ghi.jkl
abc.def.xty.xyz
abc.def.rew.ter

Can somebody help.

Thanks.
# 2  
Old 09-16-2009
you don't want the first line in the splitted file? or its a mistake.

try:

Code:
for FILE in $(ls -1 \'*)
do
 
file=${FILE#\'}
file=${file%\'}

mv -f "$FILE" "$file"

file1=$(echo "$file" | sed -e "s|^|Hdr_|g")
file2=$(echo "$file" | sed -e "s|^|Dtl_|g")

sed -ne '1,2p' "$file" > $file1
sed -ne '3,$p' "$file" > $file2
 
done


this will put first two line in one file and rest in another. (as per you code)

Last edited by clx; 09-16-2009 at 02:40 PM.. Reason: removed useless sed part
# 3  
Old 09-16-2009
I rewrote your script a little:

splittinquoted:
Code:
FILE_NAMES="$@"

if [ -f "$1" ]
then
    echo "\n Files are available to process \n"
else
    echo "\n Specified files do not exist: Process is Terminating \n"
    exit 1
fi

for quoted_file_name in $FILE_NAMES
do
  file_name=${quoted_file_name//\'/}
  mv -f $quoted_file_name $file_name
  sed -ne '1,2p' $file_name > Hdr_$file_name
  sed -ne '3,$p' $file_name > Dtl_$file_name
done

Usage: ./splittingquoted \'*\'
# 4  
Old 09-16-2009
bash solution using parameter expansion.
Code:
# ls
'abc.def.ghi.jkl'       'abc.def.rew.ter'       'abc.def.xty.xyz'

# for file in \'*;do echo mv "$file" "${file//\'/}";done
mv 'abc.def.ghi.jkl' abc.def.ghi.jkl
mv 'abc.def.rew.ter' abc.def.rew.ter
mv 'abc.def.xty.xyz' abc.def.xty.xyz

# for file in \'*;do mv "$file" "${file//\'/}";done

# ls
abc.def.ghi.jkl abc.def.rew.ter abc.def.xty.xyz

# 5  
Old 09-16-2009
yeah, its better to provide the new filenames at the time of redirection.

after elimination of :
Code:
file1=$(echo "$file" | sed -e "s|^|Hdr_|g")
file2=$(echo "$file" | sed -e "s|^|Dtl_|g")

its reduced to:

Code:
for FILE in $(ls -1 \'*)
do
 
file=${FILE#\'}
file=${file%\'}

mv -f "$FILE" "$file"

sed -ne '1,2p' "$file" > Hdr_$file
sed -ne '3,$p' "$file" > Dtl_$file
 
done

# 6  
Old 09-16-2009
Thankyou all......its working
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Renaming file and check for the renamed file existence

Hi Am trying to move a file from one name to another When I do "ls" to check for the moved filename I can see the file but when I try the same with a script am unable.. I think am doing some pretty silly error.. please help.. toMove=`ls | grep -E "partition.+"` mv $toMove partition._org... (7 Replies)
Discussion started by: Priya Amaresh
7 Replies

2. UNIX for Dummies Questions & Answers

Renaming File

Hi there, I have 350 files in this directory: /home/adams/29 that was mistakenly renamed in this format: TTFILE_BIT_638478.txt.dat I want to take out the trailing .dat so that it ends in .txt: TTFILE_BIT_638478.txt I need help please. Thank you. (6 Replies)
Discussion started by: Creems
6 Replies

3. UNIX for Dummies Questions & Answers

renaming a file

Hi I remember i have create a file "Dechistory" before a month. But i dont remember exact location. I know i need to find out first that fiiles exact location for renaming it .......but i waant to that without going on that location.......how to do that ??? (1 Reply)
Discussion started by: tusharjoshi
1 Replies

4. Shell Programming and Scripting

Renaming file

Hello, I have an outstanding issue..Iam on linux and using a putty to connect to my server and then fire our unix shell script. At location /usr/sam a file called "er 1 32.txt" out boss transfer via application. From my end on terminal when i want to transfer this file to some other location... (2 Replies)
Discussion started by: j_panky
2 Replies

5. Shell Programming and Scripting

File renaming from list of names contained in another file

I have to rename a large number of files so that the name of each file corresponds to a code number that is given side by side in a list (textfile). The list contains in column A the filename of the actual files to be renamed and in column B the name (a client code, 9 digits) that has to be... (7 Replies)
Discussion started by: netfreighter
7 Replies

6. Shell Programming and Scripting

Renaming a file use another file as a sequence calling a shl

have this shl that will FTP a file from the a directory in windows to UNIX, It get the name of the file stored in this variable $UpLoadFileName then put in the local directory LocalDir="${MPATH}/xxxxx/dat_files" that part seems to be working, but then I need to take that file and rename, I am using... (3 Replies)
Discussion started by: rechever
3 Replies

7. Red Hat

Cron issue while renaming a username

Hi All; I have a to rename a user account example User1 to User2 keeping everything same. Now I would be using usermod -l User1 User2 .However User1 has some cron jobs and by using the conventional usermod iam unable to transfer those cron jobs to User2 i.e after renaming the user. ... (4 Replies)
Discussion started by: maverick_here
4 Replies

8. UNIX for Dummies Questions & Answers

Renaming Issue..

Hi guys.. Please help me.. I am a newbie to Unix.. How to change the extension of all .txt files in my current directory and also subdirectories to .my extension all together?? Regards, Mahesh.. (2 Replies)
Discussion started by: mraghunandanan
2 Replies

9. Shell Programming and Scripting

Renaming a file

Hai masters, Suppose i have so many files named xxxx.ksh.tar, yyyy.ksh.tar and so on. My requirement : i want to rename all the files with extension ".ksh.tar" to ".tar" in my directory. Could you please tell me the solution for this.. (5 Replies)
Discussion started by: ecearund
5 Replies

10. UNIX for Dummies Questions & Answers

Help in renaming file !!!

Hi All, I want to rename a file inside a script which has a date portion appended at the start of the file name. The script i wrote works fine when the file comes on a day to day basis but sometimes it comes late too. #!/usr/bin/ksh cd /space/file/source dt=$(date "date "+%m%d%Y")... (5 Replies)
Discussion started by: kumarsaravana_s
5 Replies
Login or Register to Ask a Question