How do I take out(remove) the date part in the file name?

Hi Guys here I am again,

I have two files in a specified location.
Location ex: /opt/xdm/input/
input file names: 1. abc_app.yyyymmdd.dtd
2. abd_app.yyyymmdd.dtd
I need to build a code that reads the files from the location based on the oldest date and cuts the date part from the file name and places in the same dir.

Expected output files:
1.abc_app.dtd
2.abd_app.dtd

The "yyyymmdd" part changes everyday.So I need to find it dynamically and remove the date part from the files starting from the oldest file.

I am very much new to UNIX, can u help me out with the code?

Waiting for your reply!!!!!!!!!!!!!

ls -lrt abc*dtd | tail -1 | nawk '{ split($NF,a,"."); print $NF" "a[1]"."a[3] }' | xargs mv

ls -lrt abd*dtd | tail -1 | nawk '{ split($NF,a,"."); print $NF" "a[1]"."a[3] }' | xargs mv

Thanks for your valuable time.

I tried executing your command but it says the error as follows:

test.ksh
./test.ksh[3]: nawk: not found
Usage: mv [-f] [-i] [-e warn|force|ignore] f1 f2
mv [-f] [-i] [-e warn|force|ignore] f1 ... fn d1
mv [-f] [-i] [-e warn|force|ignore] d1 d2

I think the nawk is not installed. I dont knw whether it comes as built in or need to install seperately, but the above error I am getting and not able to understand why this is happening.

Can u pls spare some more time for me?

on HP-UX /usr/bin/awk is nawk. The old awk has been removed.

Hi Perderabo,

Thanks . You r right. That way the above command works fine.

But the problem is if I have more than one file in the input dir with the same name and with a different time stamp,
If we run this command it removes the date part of the file and places in that dir(this is for file f1)
For the second file f2, it repeats the same process and replaces the first file f1 as the file names are same without a time stamp.

The basic requirement is: Getting the file from a specific location and adding a column at the end of each line and placing the file back to the original dir.

I am using the sed command for adding a column at the end of each line.

export file=/var/opt/test/flat_file_extract.dtd
sed 's/^\(.*\)$/&\|N/' $file > file_temp
mv "file_temp" "$file

I dont knw how to incorporate the date dynamically for the sed command.

actual file name is flat_file_extract.20060127.dtd

The date stamp changes everday day and some times there may be more than 1 file with same name prefix with a different time stamp.At that time when I look for the process it should get the oldest file first and then other.

Can anybody help me out of this situation please?

If I get the jist of your issue right.

You have files having a common with date and time stamps affixed in the name. And you have to grab the oldest file first and rename to the common name with a column being appended to the lines in the file.

If I am right you have files for eg.
test_file.20060206.dtd
test_file.20060207.dtd

If that is the case.

If your files are in the current working directory then.

export comm_file_name=test_file.dtd

export curr_file=`l -ltr test_file.* | awk 'NR == 1 {printf("%s",$9);}'`

sed 's/^\(.*\)$/&\|N/' $curr_file > file_temp

mv file_temp $comm_file_name

If the files are not in your current working directory then

dir_name=/var/opt/test/

export comm_file_name=$dir_name/test_file.dtd

export curr_file=`l -ltr $dir_name/test_file.* | awk 'NR == 1 {printf("%s",$9);}'`

sed 's/^\(.*\)$/&\|N/' $curr_file > file_temp

mv file_temp $comm_file_name

I hope it works.

Rajeeb