0

For satisfying a legacy code i had to add date to a filename like shown below(its definitely needed and cannot modify legacy code :( ). But i need to remove the date within the same command without going to a new line. this command is read from a text file so i should do this within the single command.

$((echo "$file_name".`date +%Y%m%d`| sed 's/^prefix_//') 

so here i am removing the prefix from filename and adding a date appended to filename. i also do want to remove the date which i added. for ex: prefix_filename.txt or prefix_filename.zip should give me as below.

Expected output:

filename.txt
filename.zip

Current output:

filename.txt.20161002
filename.zip.20161002
  • 1
    Talking about legacy code is making your question hard to read. What's your real question. Do you have to remove the date from filenames, or just remove the prefix without adding a date? – glenn jackman Oct 03 '16 at 00:51
  • Can you echo the date string alone, without the file name or the sed command? – Beta Oct 03 '16 at 00:51
  • I need to add date to the command i mentioned and remove it again along with removing prefix which i am doing already. – Karthikeyan Chidambaram Oct 03 '16 at 01:02

5 Answers5

0

Assumming all the files are formatted as filename.ext.date, You can pipe the output to 'cut' command and get only the 1st and 2nd fields :

~> X=filename.txt.20161002
~> echo $X | cut -d"." -f1,2
filename.txt
chenchuk
  • 5,324
  • 4
  • 34
  • 41
0

I am not sure that I understand your question correctly, but perhaps this does what you want:

$((echo "$file_name".`date +%Y%m%d`| sed -e 's/^prefix_//' -e 's/\.[^.]*$//') 
Beta
  • 96,650
  • 16
  • 149
  • 150
  • What you have mentioned is correct, but the real filename may have multiple periods in filename like (prefix_original.report.status.txt or prefix_ooriginal.report.zip or prefix_original.status.DAT) – Karthikeyan Chidambaram Oct 03 '16 at 01:17
  • @KarthikeyanChidambaram: And what do you want the output to be in those cases? And have you tested this solution in those cases? – Beta Oct 03 '16 at 17:20
0

Sample input:

cat sample
prefix_original.txt.log.tgz.10032016
prefix_original.txt.log.10032016
prefix_original.txt.10032016
prefix_one.txt.10032016
prefix.txt.10032016
prefix.10032016

grep from start of the string till a literal dot "." followed by digit.

grep -oP '^.*(?=\.\d)' sample
prefix_original.txt.log.tgz
prefix_original.txt.log
prefix_original.txt
prefix_one.txt
prefix.txt
prefix

perhaps, following should be used:

grep -oP '^.*(?=\.\d)|^.*$' sample 
P....
  • 17,421
  • 2
  • 32
  • 52
0

If I understand your question correctly, you want to remove the date part from a variable, AND you already know from the context that the variable DOES contain a date part and that this part comes after the last period in the name.

In this case, the question boils down to removing the last period and what comes after.

This can be done (Posix shell, bash, zsh, ksh) by

 filename_without=${filename_with%.*}

assuming that filename_with contains the filename which has the date part in the end.

user1934428
  • 19,864
  • 7
  • 42
  • 87
0
  % cat example
filename.txt.20161002
filename.zip.20161002

  % cat example  |  sed "s/.[0-9]*$//g"
filename.txt
filename.zip

 %
Harini
  • 551
  • 5
  • 18
User9102d82
  • 1,172
  • 9
  • 19
  • While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – kayess Jun 01 '17 at 09:44