while running the above code getting below error.Please help.
******************************
file names
Moderator's Comments:
Please use CODE tags when displaying sample input, sample output, and code segments; not just code segments.
There are almost certainly much simpler ways to do what you're trying to do, but we need to know what operating system and shell you're using to be able to suggest a solution that will work in your environment. Whenever you start a new thread in UNIX for Beginners Questions & Answers forum, it always helps us help you if you tell us what operating system and shell you're using!
I note that you're trying to use awk's gsub() function which will replace all occurrences of file with newfile. And I note that none of your filenames contains more than one occurrence of that string. If that string does occur more than once in a filename, do you really want to replace all of them? Or, do you just want to change the first occurrence?
The error you're getting is because you haven't quoted the awk script operand and the shell is seeing the parentheses on the call to gsub() as an attempt to invoke a sub-shell at a spot where a sub-shell is not valid.
If you fix that and you ever try passing a filename to that script that does not contain the string file, the mv command for that file will fail because the variable t will be set to an empty string. If you make sure that your awk script will always return the original filename if no occurrences of the string file were found, you would end up trying to issue a command similar to:
which will be a relatively slow no-op; but at least it won't cause your script to issue diagnostics for missing operands.
The command substitution you're using is an obsolescent form (unless you're using a pure Bourne shell from the 1970's. And, there is no reason to invoke a sub-shell in your command substitution.
Does the operating system you're using include a utility named rename?
This User Gave Thanks to Don Cragun For This Post:
In many operating systems, there is a utility in the repositories - a script written in Perl. Copes with such tasks very well. --- Post updated at 19:00 ---
Oh, at first it's better to look at the output without real renaming.
however i want to solve that by using sed or awk.
....
The necessary modifications to your own loop were presented to you, to make it run, and to improve it. However, no sed nor awk are needed to solve your problem. Just use shell's "parameter expansion", and you're there:
you could use an if ... then ... fi construct as well. Remove the echo if you're happy with what you see.
You never answered the question of whether a file named filexfile.txt should be moved to newfilexfile.txt or to newfilexnewfile.txt. And, I'm disappointed that you didn't try to rewrite your code based on the comments I provided in post #2 in this thread. But, I did make (at least) one mistake in that post. If you run the command:
where file1 and file2 are the same and name an existing file or if file1 and file2 are different but both names are pathnames that refer to the same file (because they are linked together), the standards require that one of three things happen:
the request is silently ignored,
a diagnostic message is printed, no change is made to the file, and the final exit status of mv will be non-zero, or
if the two operands are distinct (but name the same file), the file1 will be removed. (Note that if file2 is a symbolic link pointing to file1 and file1 is not a symbolic link, this may result in the loss of your data.)
Note that none of the suggestions that have been posted in this thread (including the two suggestions below) make any attempt to verify that the new pathname you want to use is not linked to the current pathname. The code below at least makes sure that when mv is invoked, the two operands won't be the same string. If there is a chance that the old and new names you are trying to create for a file could refer to the same file, I leave it to you to make adjustments to the code if any system on which you run your script has a mv utility that does not behave the way you want it to behave.
If you really don't want to use the shell's "${path/file/newfile}" variable expansion because it is not in the standards yet or because you really do want to be able to replace multiple occurrences of file with newfile if you run into a pathname containing multiple occurrences of that string, and you really want to use
or
to solve this problem; at least consider something like the following so you aren't invoking awk or sed and a shell for each pathname you process and so you don't invoke mv to move a file whose name won't change because it didn't contain the string file to begin with:
or (preferably):
If one of these prints the mv commands you want to invoke, remove the word echo from the script and run it again to actually rename the files.
Obviously, the awk script could also be rewritten to get rid of the second invocation of bash. I will leave doing that set of trivial changes as an exercise for the reader.
This User Gave Thanks to Don Cragun For This Post:
Hi,
In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this?
I am using #!/bin/ksh
For eg: sftp user@host <<EOF
cd /path
get *.txt
rename *.txt *.txt.done
... (7 Replies)
Hi,
I have a list of files in a folder with the same name ending (over 1000 files)
joe.jpy.jpeg
joe1.jpy.jpeg
joe2.jpy.jpeg
jon3.jpy.jpeg
jor5.jpy.jpeg
.....jpy.jpeg
etc.
I want to change jpy to hhk
So the output will be:
joe.hhk.jpeg
joe1.hhk.jpeg
joe2.hhk.jpeg
jon3.hhk.jpeg... (3 Replies)
Dear All expertise..
just wanna ask if there is a way on how to change the filename as per below sample:-
filename:-
cob120514093156.03.466926330
CCBS-CPMM_SEND_COB_120514_100549_3.rpt
to convert to:-
cob120514093156.03.466926330.bd (rename from cob120514093156.03.466926330)
... (17 Replies)
Hi All
I have a folder that contains hundreds of file with a names
3.msa
4.msa
21.msa
6.msa
345.msa
456.msa
98.msa
...
...
...
I need rename each of this file by adding "core_" in the begiining of each file such as
core_3.msa
core_4.msa
core_21.msa (4 Replies)
Hello,
I wanted to rename one file where filename contains space.. How can i rename in unix?
The file name is ABC XYZ.TXT
I wanted to rename this file as ABCXYZ.TXT.
Any help is greatly appreciated...
Regards. (4 Replies)
I just can't figure this one out.
I have a lot of files name (for example) ABC1234.5678.ext
I need to rename these files U0105678PQRS
So I'm removing everything before the first "."
I'm keeping "5678" in the file name
Adding "U010" to the front of "5678"
Dropping the ".ext" extension
... (5 Replies)
Hi All,
I have a folder name as postscript folder and it contains the following postscript files.
package1.ps
package2.ps
package3.ps
when i am renaming all the ps files to xps files by using the following command
mv /postscript/*.ps /postscript/*.xps
Then i am getting the... (4 Replies)
Hi Alll,
I have a script that we use on the servers to change the name of files that have spaces in the name:
#!/bin/tcsh
set n = 0
foreach f ( * )
echo $f | grep " "
if ( $? == 0 ) then
mv "$f" `echo $f | sed -e "s/ /_/g"`
@ n += 1
endif
end
echo $n changed
I need to write a... (2 Replies)
I have a list of files named ab_*.csv
I would like to remane them all by removing the ab_ and have *.csv
I did the following but I am surely missing something.
/* wrong script */
for i in `ls -1 ab_*`; do mv ab_$i $i; done
Thanks in advance. (1 Reply)
hi,
I have the following files as below.
reg_test_123232
reg_test_125654
reg_test_473843
How do I rename reg_test_123232 to abc_123232 and the rest of file by just keeping the numeric field ?
Please advise :) (3 Replies)