file name manipulation

file name manipulation

Post by Dyip » Sat, 06 Jan 1996 04:00:00



Happy new year, help me with this please.

I am looping though directories picking up file name and then rename them
afterward.  I am looking for the proper and easiest way of doing this:

#!/bin/sh
# Pick up all files with name starts with somefile and end with upper case
A
for  FILE_NAME in $DIRECTORY/somfile*A
do
    #  Would like to set a variable call OUTFILE base on the same input
file name
    #  but end with something else instead of A, say X.
    #  Assume I can't use cut because I don't know the length of the file
name
    #  it may varies.  Please HELP.
    OUTPUT_FILE=??????$FILE_NAME????X
done

A step futher, say the file name indicate month, example: FILE950529 means
this file is created in May 29, 95.  I like to be able to pick up 05 and
then assign
it to a variable or something.  In assence, what I am trying to do is
reading a
bunch of files from 1 directory and base on the name determine where I am
going to put it and rename it.   I will have 12 sub-directories ../JAN/
to ../DEC/.
So, for this example, the file FILE950529 will ended up in
../MAY/FILE950529.
How do I do it ??

Thanks for the net help.

P.S. I guess I am not real good at tr, sed and awk if I should use them
together
       or soemthing to achieve this.

--------------------------------------------------------------------------
---------------
Dominic Yip - Senior System Engineer, Prism Soulution Inc.


 
 
 

file name manipulation

Post by Ale » Sat, 06 Jan 1996 04:00:00



>Happy new year, help me with this please.

You to, and all the rest reading this.

>I am looping though directories picking up file name and then rename them
>afterward.  I am looking for the proper and easiest way of doing this:
>#!/bin/sh
># Pick up all files with name starts with somefile and end with upper case
>A
>for  FILE_NAME in $DIRECTORY/somfile*A
>do
>    #  Would like to set a variable call OUTFILE base on the same input
>file name
>    #  but end with something else instead of A, say X.
>    #  Assume I can't use cut because I don't know the length of the file
>name
>    #  it may varies.  Please HELP.
>    OUTPUT_FILE=??????$FILE_NAME????X
>done
>A step futher, say the file name indicate month, example: FILE950529 means
>this file is created in May 29, 95.  I like to be able to pick up 05 and
>then assign
>it to a variable or something.  In assence, what I am trying to do is
>reading a
>bunch of files from 1 directory and base on the name determine where I am
>going to put it and rename it.   I will have 12 sub-directories ../JAN/
>to ../DEC/.
>So, for this example, the file FILE950529 will ended up in
>../MAY/FILE950529.
>How do I do it ??
>Thanks for the net help.
>P.S. I guess I am not real good at tr, sed and awk if I should use them
>together
>       or soemthing to achieve this.
>--------------------------------------------------------------------------
>---------------
>Dominic Yip - Senior System Engineer, Prism Soulution Inc.



It is *not* the only way to do it, but I would probably use ksh.

For instance: You don't know the length of the filename but you do
know it ends with "A" then

#!/bin/ksh

for FILENAME in $DIR/somefile*A;do
    OUTFILE=${INFILE%A}X
    ... do somthing with $OUTFILE ...
done

To move fileYYMMDD to /path/MMM/fileYYMMDD do

#!/bin/ksh

#
# assume filename is in $1 and is correct
# assume filename = whateverthereishere951129 in the comments
FILENAME=$1
BASENAME=${FILENAME%[0-9][0-9][0-9][0-9][0-9][0-9]}
#
# basename will be "whateverthereishere"
DATE=${FILENAME#$BASENAME}
#
# date contains 951129
#
YEAR=${DATE%[0-9][0-9][0-9][0-9]}
DAY=${DATE#[0-9][0-9][0-9][0-9]}
MONTH=${DATE%$DAY}; MONTH=${MONTH#$YEAR}
#
# YEAR=95 MONTH=11 DAY=29
#
set -A monthnames dummy JAN FEB MAR APR JUN .... NOV DEC

mv $FILENAME /where/ever/${monthnames[$MONTH]}

 
 
 

file name manipulation

Post by Tom Sande » Sun, 07 Jan 1996 04:00:00


|>
|> Happy new year, help me with this please.
|>
|> I am looping though directories picking up file name and then rename them
|> afterward.  I am looking for the proper and easiest way of doing this:
|>
|> #!/bin/sh
|> # Pick up all files with name starts with somefile and end with upper case
|> A
|> for  FILE_NAME in $DIRECTORY/somfile*A
|> do
|>     #  Would like to set a variable call OUTFILE base on the same input
|> file name
|>     #  but end with something else instead of A, say X.
|>     #  Assume I can't use cut because I don't know the length of the file
|> name
|>     #  it may varies.  Please HELP.
|>     OUTPUT_FILE=??????$FILE_NAME????X
|> done
|>

One easy way is to use sed with a $ which anchors the character substition to the end of the string.  For example this will convert any FILENAME ending in "A" to the same filename ending in X (you can add more -e substitions if you want):  

OUTPUT_FILE=`echo $FILE_NAME | sed -e 's/A$/X/' `

|> A step futher, say the file name indicate month, example: FILE950529 means
|> this file is created in May 29, 95.  I like to be able to pick up 05 and
|> then assign
|> it to a variable or something.  In assence, what I am trying to do is
|> reading a
|> bunch of files from 1 directory and base on the name determine where I am
|> going to put it and rename it.   I will have 12 sub-directories ../JAN/
|> to ../DEC/.
|> So, for this example, the file FILE950529 will ended up in
|> ../MAY/FILE950529.
|> How do I do it ??

Assuming the last 6 digits of the file are the date this will break the filename up into fields:

        revfile=`echo $FILENAME | rev`           #reverse string
        year=`echo $revfile | cut -c5-6 | rev`   #get field using cut then
        month=`echo $revfile | cut -c3-4 | rev`  #  reverse to original order
        day=`echo $revfile | cut -c1-2 | rev`
        filename=`echo $revfile | cut -c7- |rev` #

IN bourne shell the only way I can think of to convert a numeric month to an alph month is to use a case statement.  Other shells have arrays available.

        case $month in
        01)
           MNTH="JAN"
           ;;
        02)
           MNTH="FEB"
           ;;

        ...

        12)
           MNTH="DEC"
           ;;
        esac

|>
|> Thanks for the net help.
|>
|> P.S. I guess I am not real good at tr, sed and awk if I should use them
|> together
|>        or soemthing to achieve this.
|>
|> --------------------------------------------------------------------------
|> ---------------
|> Dominic Yip - Senior System Engineer, Prism Soulution Inc.


Hope this helps,
Tom Sanders

 
 
 

1. file manipulation to get several output file out of 1 file

I am trying to manipulate a file such that I can get several output
depending on first column of my file.

I have 2 columes in my file.

x   x1

x   x2

x   x3

y    y1

y    y2

...

I want to create several files such that file x(first column) contains
x1, x2, x3...(2nd column).

Thanks for your help.

Jae

2. bttv

3. how to extract path/file name upto the mzximum of two levels from the file name?

4. Optical drive support

5. Function name manipulation

6. I think I've found a 1.1.4x scsi problem.

7. Restore (file by file) and many named files

8. Linux rescue floppy

9. Examples Please: Bash Script doing File Manipulation

10. Object File Manipulation

11. writing functions for file manipulation

12. file manipulation/ram usage questions...

13. ELF file manipulation: extract .o from .so possible?