Remove the leading and trailing date from a CSV file

I'm a newbie to shell scripting.

Can anyone help with the below requirement ?

The leading and trailing date of a files to be removed.

2017-07-12_gmr_tag_log_20170711.csv
2017-07-12_gmr_call_log_20170711.csv
2017-07-12_gmr_outgoing_log_20170711.csv

I'm looking for output like this.

gmr_tag_log.csv
gmr_call_log.csv
gmr_outgoing_log.csv
sed 's/[^_]*_//; s/_[^._]*[.]\(.*\)$/.\1/' infile
1 Like

Thank you rdrtx1.

I have copied current date files to a directory, so I have to implement this requirement (leading and trailing date to be removed).

Appreciate your help.

Hello shivamayam,

Could I, first of all, encourage you to press the :b: Thanks button for rdrtx1's very useful post.

Getting to your issue, what have you got to work it in to? There must be something else that you are running that you want to use it with. What have you tried so far?

It would be good to know your OS version and your preferred tools (e.g. ksh, bash, perl etc.) so we can help you fit it in a way that you can support in future.

Kind regards,
Robin

Try using bash substrings:

#run interactively
 a="2017-07-12_gmr_tag_log_20170711.csv" 
 echo $a                                 
#remove the first 11 characters           
b=${a:11}                               
echo $b
#set l to the length of string b
 l=${#b}                                 
echo $l
#set s to the middle length of string b
let s=$l-13                             
echo $s
#set c to the middle part of the string  
c=${b:0:$s}                             
echo $c                                 
#concatenate string c and ".csv"         
d=$c.csv                                
echo $d        

I'm using a shell script. I have extracted today files to a directory1 and date should be removed on both sides of a CSV file.

FYI... I'm looking to remove the date from the file name and not inside the CSV file.

Directory1

2017-07-12_gmr_tag_log_20170711.csv
2017-07-12_gmr_call_log_20170711.csv
2017-07-12_gmr_outgoing_log_20170711.csv

Final output Directory2:

gmr_tag_log.csv
gmr_call_log.csv
gmr_outgoing_log.csv

---------- Post updated at 09:30 AM ---------- Previous update was at 03:52 AM ----------

I used the below script and it removed the trailing date. How to remove the leading date ?

ls -1 *.csv | awk '{print "mv " $1 " final/"$1}' | sed -E 's/([[:alpha:]]+)_[^_]+(\.csv)/\1\2/2' | csh

Any help is much appreciated.

#!/bin/bash
for file in *.csv
do
   len=${#file}
   let len=$len-11-13
   outfile=${file:11:$len}
   cp $file outdir/$outfile.csv
done

Change cp to mv when you have tested.

1 Like

Thank you jgt.