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


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Operating Systems HP-UX How do I take out(remove) the date part in the file name?
# 1  
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!!!!!!!!!!!!!
# 2  
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
# 3  
Hi Mahendra

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?
# 4  
on HP-UX /usr/bin/awk is nawk. The old awk has been removed.
# 5  
The basic requirement

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?
# 6  
Here it is

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
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #1
Difficulty: Medium
The UNIX and Linux Forums (UNIX.com) was founded in 1985.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove part of the file using a condition

Gents, Is there the chance to remove part of the file, Taking in consideration this condition. For each record the first row start with the string % VE should be 56 rows for each records.. first row = % VE last row = % sw total 56 rows for each record. Then in the case that the... (4 Replies)
Discussion started by: jiam912
4 Replies

2. Shell Programming and Scripting

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

Hi All, I need to create links between two directories. have multiple files in a specified location. Source Location ex: /opt/xdm/input/ Target Location ex: /opt/xdm input file names: 1. abc_app.aus.apac.yyyymmdd.dtd 2. abcd_app.aus.apac.yyyymmdd.dtd I need to build a code that reads... (1 Reply)
Discussion started by: kthri_82
1 Replies

3. Shell Programming and Scripting

How to remove the date part of the file?

Hi Gurus, I have file name like: abcd.20131005.dat I need to remove mid part of data final name should be abcd.dat thanks in advance (2 Replies)
Discussion started by: ken6503
2 Replies

4. Shell Programming and Scripting

Remove part of a file based on identifiers

here below is a part of the file cat fileName.txt NAME=APP-VA-va_mediaservices-113009-VA_MS_MEDIA_SERVER_NOT_PRESENT-S FIXED=false DATE= 2013-02-19 03:46:04.4 PRIORITY=HIGH RESOURCE NAME=ccm113 NAME=APP-DS-ds_ha-140020-databaseReplicationFailure-S FIXED=false DATE= 2013-02-19... (4 Replies)
Discussion started by: vivek d r
4 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Rename file name / remove part of name

I have a whole file structure with jpeg files where I want to remove a part of the file name. An application added in many files a case conflict in the naming "xyz 017.jpg (Case Conflict 1)" So, can someone help me how to get rid of the " (Case Conflict 1)"? What I have is this: find . -name... (2 Replies)
Discussion started by: borobudur
2 Replies

6. UNIX for Dummies Questions & Answers

Remove part of file names in a directory

Hi, i have a directory full of pictures, .jpg files. Half of them begin with "beach_" (for ex beach_123_123456.jpg) i'm looking for a command to remove the "beach_" from all files in the directory. thanks (4 Replies)
Discussion started by: robertmanalio
4 Replies

7. Shell Programming and Scripting

Remove part of a column in a text file

I have a text file with delimiter "|" and sometimes the zipcode is in "5th" column or "6th" column. I want to scan the file and remove the "-2323" from the zipcode which has zip+4 digits From blah|blah|foo|bar||blah|945523-232|USA blah|blah|foo|bar||blah|foo|94555-2323|USA To... (8 Replies)
Discussion started by: gubbu
8 Replies

8. Shell Programming and Scripting

remove range part of a file with sed

Hello, I would like to remove a range from a file with sed or any script command that is appropriate The section start by and finish by and I would like to keep line Could you tell me which command I should type ? Thanks a lot, Franck My input file is like this... (1 Reply)
Discussion started by: mfranck
1 Replies

9. Shell Programming and Scripting

Remove part of the file

I have an xml file, from where I need to take out Application2 entries and keep the others. I need to remove from <product> to </product> and the key element to look for while removing should be <application> as other pairs can be same for others. <product> ... (10 Replies)
Discussion started by: chiru_h
10 Replies

10. Shell Programming and Scripting

remove certain part of file name

Hi, Is it possible to remove the first part of the file name using find. i.e i have something like 2006abc.txt , 1007bed.txt etc, I wanna rename them to abc.txt , bed.txt I tried some stupid way.. find . -name '*.txt' -exec mv {} `cut -f2-5 -d"_" {}` \; somehow iam not getting it. ... (3 Replies)
Discussion started by: braindrain
3 Replies

Featured Tech Videos