Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Remove the leading and trailing date from a CSV file

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-12-2017
shivamayam shivamayam is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 11 October 2017, 12:51 PM EDT
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
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.


Code:
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.


Code:
gmr_tag_log.csv
gmr_call_log.csv
gmr_outgoing_log.csv


Last edited by rbatte1; 07-12-2017 at 12:17 PM.. Reason: Added CODE tags
Sponsored Links
    #2  
Old Unix and Linux 07-12-2017
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 19 October 2017, 2:34 PM EDT
Location: Houston, Texas, USA
Posts: 971
Thanks: 0
Thanked 332 Times in 314 Posts

Code:
sed 's/[^_]*_//; s/_[^._]*[.]\(.*\)$/.\1/' infile

The Following User Says Thank You to rdrtx1 For This Useful Post:
shivamayam (07-12-2017)
Sponsored Links
    #3  
Old Unix and Linux 07-12-2017
shivamayam shivamayam is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 11 October 2017, 12:51 PM EDT
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
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.

Last edited by shivamayam; 07-13-2017 at 07:33 AM..
    #4  
Old Unix and Linux 07-12-2017
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 20 October 2017, 11:53 AM EDT
Location: Lancashire, UK
Posts: 3,307
Thanks: 1,417
Thanked 648 Times in 584 Posts
Hello shivamayam,

Could I, first of all, encourage you to press the Linux 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
Sponsored Links
    #5  
Old Unix and Linux 07-12-2017
jgt's Unix or Linux Image
jgt jgt is online now Forum Advisor  
Registered User
 
Join Date: Apr 2007
Last Activity: 20 October 2017, 3:40 PM EDT
Location: 44.21.48N 80.50.15W
Posts: 1,989
Thanks: 2
Thanked 268 Times in 251 Posts
Try using bash substrings:

Code:
#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

Sponsored Links
    #6  
Old Unix and Linux 07-13-2017
shivamayam shivamayam is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 11 October 2017, 12:51 PM EDT
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
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.

Last edited by shivamayam; 07-13-2017 at 07:33 AM..
Sponsored Links
    #7  
Old Unix and Linux 07-13-2017
jgt's Unix or Linux Image
jgt jgt is online now Forum Advisor  
Registered User
 
Join Date: Apr 2007
Last Activity: 20 October 2017, 3:40 PM EDT
Location: 44.21.48N 80.50.15W
Posts: 1,989
Thanks: 2
Thanked 268 Times in 251 Posts

Code:
#!/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.
The Following User Says Thank You to jgt For This Useful Post:
shivamayam (07-13-2017)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Clearing leading and trailing blanks from a string in C Shell phudgens Shell Programming and Scripting 1 05-01-2012 05:44 PM
Help to move leading negative sign to trailing position selvankj Shell Programming and Scripting 11 07-07-2011 08:35 AM
Removing leading and trailing spaces only in PERL kumar04 Shell Programming and Scripting 2 04-04-2009 03:17 PM
remove trailing and leading spaces using tr command sureshg_sampat Shell Programming and Scripting 5 03-04-2009 04:04 AM
Leading and Trailing Spaces sleepster Shell Programming and Scripting 7 10-29-2003 10:48 PM



All times are GMT -4. The time now is 04:02 PM.