Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Korn script syntax question

Status
Not open for further replies.

heprox

IS-IT--Management
Dec 16, 2002
178
US
I'm using a Korn script to rename some files using a simple function. Basically I have large set of files with the following format:

str####.asc.????? (example: str0002.asc.23154)

#### = a location number
????? = the PID of the process that created this file

...I want to rename these files to the following format:

upload.#### (example: upload.0002)

...and just loose the PID all together. I'm using the following syntax, but I still can't get it to only "grab" the location number:


while :
do
for fileName in /datafiles/str[0-9][0-9][0-9][0-9].asc.?????
do
fileExtension=${fileName#*.}
is_file_arrived "$fileName" && processFile $fileName $fileExtension
done
done

...can someone tell me the proper syntax for:

"fileExtension=${fileName#*.}"

...also how would I put in a check to see if the file that it was being renamed too, already exists?
 
Try something like this:
newFile=$(echo $fileName | sed 's!str!upload.!;s!\.asc\..*!!')
[ -f $newFile ] && echo "$newFile already exists" || mv $fileName $newFile

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
That looks like a good solution for the check to see of that file already exists, but any idea what the expression needs to be for me to get the location numbers only?
 
With a very minimal effort (tip: man sed) you may amend the first line of the code snippet I gave you and so retrieve the location number you are looking for.
 
I realize that I could do this with SED and I have looked at the pages, but is the line,

"fileExtension=${fileName#*.}"


....using SED? If not, then why bother..., when what I'm trying to accomplish is just to attach the location number ONLY to the variable $fileExtension. I'm trying to understand the need for using the SED command that didn't previously exist in my (working) script?
 
I answered to this question:
...I want to rename these files to the following format:
upload.#### (example: upload.0002)

With your example (fileName=str0002.asc.23154)
${fileName#*.} --> asc.23154
${fileName##*.} --> 23154
So, what do you really want to do ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I need to attach just the location number, so if my file is called,

"(fileName=str0002.asc.23154)"

I want the variable "$fileExtension" to be "0002" and forget about everything else. I already tried:

${fileName#*.} --> asc.23154
${fileName##*.} --> 23154

..with the same results, I just can't get it to only "grab" the "0002"?
 
You can either try this:
t=${fileName#str};fileExtension=${t%%.*}
or this:
fileExtension=$(echo $fileName | sed 's!str!!;s!\..*!!')

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks for your help, worked like a charm and I now understand the syntax much better...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top