Another scripting Question...
Thanks again for the previous help. That went so quick, maybe another one? I'm looking for a simple way to strip the first 8 characters of every file in a directory. This one has me confused, I admit. I know it can be done... just not sure how. Thanks in advance. -- David Crouch
On Sat, 2005-12-03 at 21:15, David Crouch wrote:
Thanks again for the previous help. That went so quick, maybe another one?
I'm looking for a simple way to strip the first 8 characters of every file in a directory. This one has me confused, I admit. I know it can be done... just not sure how.
"cut" will do what you want. See 'man cut' -- Jim Cunning <jcunning@cunning.ods.org>
On Sat, 2005-12-03 at 21:33, Jim Cunning wrote:
On Sat, 2005-12-03 at 21:15, David Crouch wrote:
Thanks again for the previous help. That went so quick, maybe another one?
I'm looking for a simple way to strip the first 8 characters of every file in a directory. This one has me confused, I admit. I know it can be done... just not sure how.
"cut" will do what you want. See 'man cut'
Actually I might have answered a bit too quickly, because you didn't give enough information. You want to strip the first 8 characters of each file's contents, or the 1st 8 of the files' names? In either case, the solution is more involved than my original answer implied--and also different depending on your objective. -- Jim Cunning <jcunning@cunning.ods.org>
On 12/3/05, Jim Cunning <jcunning@cunning.ods.org> wrote:
On Sat, 2005-12-03 at 21:33, Jim Cunning wrote:
On Sat, 2005-12-03 at 21:15, David Crouch wrote:
Thanks again for the previous help. That went so quick, maybe another one?
I'm looking for a simple way to strip the first 8 characters of every file in a directory. This one has me confused, I admit. I know it can be done... just not sure how.
"cut" will do what you want. See 'man cut'
Actually I might have answered a bit too quickly, because you didn't give enough information. You want to strip the first 8 characters of each file's contents, or the 1st 8 of the files' names? In either case, the solution is more involved than my original answer implied--and also different depending on your objective. -- Jim Cunning <jcunning@cunning.ods.org>
Let me clarify. I want to strip the first 8 characters off each file in a given file name. Sorry for the confusion. Cut works, partly, I've figured out how to strip the charazcters, but not how to rename with the results. -- David Crouch
David, On Saturday 03 December 2005 21:48, David Crouch wrote:
...
Let me clarify. I want to strip the first 8 characters off each file in a given file name. Sorry for the confusion. Cut works, partly, I've figured out how to strip the charazcters, but not how to rename with the results.
Here's one solution (call it "chop8"): -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==- #!/bin/bash --norc for arg; do case "$arg" in ?????????*) newName="${arg#????????}" # echo "old=\"$arg\"; new=\"$newName\"" if [ -f "$newName" ]; then echo "chop8: Chopped name \"$newName\" names an existing file" >&2 continue; else mv "$arg" "$newName" fi ;; *) echo "chop8: File name \"$arg\" is less than nine characters long" >&2 ;; esac done -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==- Testing: % touch 12345678ABC 12345678BCD 12345678CDE 1 12 123 1234 12345 123456 1234567 12345678 123456789 87654321ABC 87654321BCD 87654321CDE % ls -1 1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678ABC 12345678BCD 12345678CDE 87654321ABC 87654321BCD 87654321CDE % chop8 * chop8: File name "1" is less than nine characters long chop8: File name "12" is less than nine characters long chop8: File name "123" is less than nine characters long chop8: File name "1234" is less than nine characters long chop8: File name "12345" is less than nine characters long chop8: File name "123456" is less than nine characters long chop8: File name "1234567" is less than nine characters long chop8: File name "12345678" is less than nine characters long old="123456789"; new="9" old="12345678ABC"; new="ABC" old="12345678BCD"; new="BCD" old="12345678CDE"; new="CDE" old="87654321ABC"; new="ABC" old="87654321BCD"; new="BCD" old="87654321CDE"; new="CDE" Now, running it for real (echo commented, comment on mv removed): % chop8 * chop8: File name "1" is less than nine characters long chop8: File name "12" is less than nine characters long chop8: File name "123" is less than nine characters long chop8: File name "1234" is less than nine characters long chop8: File name "12345" is less than nine characters long chop8: File name "123456" is less than nine characters long chop8: File name "1234567" is less than nine characters long chop8: File name "12345678" is less than nine characters long chop8: Chopped name "ABC" names an existing file chop8: Chopped name "BCD" names an existing file chop8: Chopped name "CDE" names an existing file % ls -1 1 12 123 1234 12345 123456 1234567 12345678 87654321ABC 87654321BCD 87654321CDE 9 ABC BCD CDE
David Crouch
Randall Schulz
David Crouch wrote:
On 12/3/05, Jim Cunning <jcunning@cunning.ods.org> wrote:
On Sat, 2005-12-03 at 21:15, David Crouch wrote:
Thanks again for the previous help. That went so quick, maybe another one?
I'm looking for a simple way to strip the first 8 characters of every file in a directory. This one has me confused, I admit. I know it can be done... just not sure how. "cut" will do what you want. See 'man cut' Actually I might have answered a bit too quickly, because you didn't give enough information. You want to strip the first 8 characters of each file's contents, or the 1st 8 of the files' names? In either case,
On Sat, 2005-12-03 at 21:33, Jim Cunning wrote: the solution is more involved than my original answer implied--and also different depending on your objective. -- Jim Cunning <jcunning@cunning.ods.org>
Let me clarify. I want to strip the first 8 characters off each file in a given file name. Sorry for the confusion. Cut works, partly, I've figured out how to strip the charazcters, but not how to rename with the results.
What are you renaming? The new file? Why should that be a problem? You should have the original filename as one of the command parameters. Perhaps if you were to give an example of what you're trying to do, as right now you're generating more questions than answers.
participants (4)
-
David Crouch
-
James Knott
-
Jim Cunning
-
Randall R Schulz