Remove the dates from the file name

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Remove the dates from the file name

Karthik
Hi Guru,

Could you please help me with the below one?

I want to remove the date/numbers from the below files


ReplicateDPS1_CS_FISH_20181228_165040.txt
Reverse_ReplicateDPS1_CS_FISH_20181228_165040.txt
SnapshotReplicateDPS1_CS_FISH_20181228_165040.txt
ReplicateDPS1_LESS_180_DAYS_SCHOOL_YEAR_20181228_165040.txt
ReplicateDPS2_BENEFITS_20181228_165040.txt


Expected Output:

ReplicateDPS1_CS_FISH.txt
Reverse_ReplicateDPS1_CS_FISH.txt
SnapshotReplicateDPS1_CS_FISH.txt
ReplicateDPS1_LESS_180_DAYS_SCHOOL_YEAR.txt
ReplicateDPS2_BENEFITS.txt

Reply | Threaded
Open this post in threaded view
|

Re: Remove the dates from the file name

Guru
Administrator
If you have GNU sed:

$ sed -r 's/[0-9]{8}_[0-9]{6}//' file
ReplicateDPS1_CS_FISH_.txt
Reverse_ReplicateDPS1_CS_FISH_.txt
SnapshotReplicateDPS1_CS_FISH_.txt
ReplicateDPS1_LESS_180_DAYS_SCHOOL_YEAR_.txt
ReplicateDPS2_BENEFITS_.txt

If not GNU:

$ sed 's/[0-9]\{8\}_[0-9]\{6\}//' file
ReplicateDPS1_CS_FISH_.txt
Reverse_ReplicateDPS1_CS_FISH_.txt
SnapshotReplicateDPS1_CS_FISH_.txt
ReplicateDPS1_LESS_180_DAYS_SCHOOL_YEAR_.txt
ReplicateDPS2_BENEFITS_.txt
Reply | Threaded
Open this post in threaded view
|

Re: Remove the dates from the file name

Karthik
Thanks for your response Guru..

i dont have GNU sed, so i tried your second option, but it is not making any change to the file name, instead it displayed the contents of the file.

Then i tried below, it is not making any change to the file name.

sed -i 's/[0-9]\{8\}_[0-9]\{6\}//' Reverse_ReplicateDPS1_CS_FISH_20181228_165040.txt

Also, please see my expected output and the outputs from your response, extra '_' is there in your outputs.

Could you please help me?


My Expected Output:


ReplicateDPS1_CS_FISH.txt
Reverse_ReplicateDPS1_CS_FISH.txt
SnapshotReplicateDPS1_CS_FISH.txt
ReplicateDPS1_LESS_180_DAYS_SCHOOL_YEAR.txt
ReplicateDPS2_BENEFITS.txt

Reply | Threaded
Open this post in threaded view
|

Re: Remove the dates from the file name

Karthik
Hi Guru,

When you get some time, can you share your idea please?
Reply | Threaded
Open this post in threaded view
|

Re: Remove the dates from the file name

Guru
Administrator
Just need to add an _ in the beginning:

sed 's/_[0-9]\{8\}_[0-9]\{6\}//'  file

Reply | Threaded
Open this post in threaded view
|

Re: Remove the dates from the file name

Karthik
Hello Guru,
Thanks for your response.

below command is working as expected, but it is displaying the file name without the dates in the command line. But i need to apply the change to the file name permanently. Can you please help me?

echo 'ReplicateDPS1_LESS_180_DAYS_SCHOOL_YEAR_20181228_165040.txt' | sed 's/_[0-9]\{8\}_[0-9]\{6\}//'