Remove the leading and trailing date from a CSV file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove the leading and trailing date from a CSV file
# 1  
Old 07-12-2017
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 01:17 PM.. Reason: Added CODE tags
# 2  
Old 07-12-2017
Code:
sed 's/[^_]*_//; s/_[^._]*[.]\(.*\)$/.\1/' infile

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 07-12-2017
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 08:33 AM..
# 4  
Old 07-12-2017
Hello shivamayam,

Could I, first of all, encourage you to press the Smilie 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
# 5  
Old 07-12-2017
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

# 6  
Old 07-13-2017
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 08:33 AM..
# 7  
Old 07-13-2017
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.
This User Gave Thanks to jgt For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove leading and trailing spaces for variable in shell script?

Hi I have variable named tablename. The value to tablename variable has leading and trailing white spaces. How to remove the leading and training white spaces and write the value of the tablename without space to a file using shell script. ( for e.g. tablename= yyy ) INPUT ... (10 Replies)
Discussion started by: pottic
10 Replies

2. Shell Programming and Scripting

Remove leading and trailing spaces from a file

Hi, I am trying to remove leading and trailing spaces from a file using awk but somehow I have not been able to do it. Here is the data that I want to trim. 07/12/2017 15:55:00 |entinfdev |AD ping Time ms | .474| 1.41| .581|green |flat... (9 Replies)
Discussion started by: svajhala
9 Replies

3. Shell Programming and Scripting

Ho to remove leading zeros from a csv file which is sent from a UNIX script

Hi All, I am using a informatica job to create a csv file and a unix script the mail the generated file.Everything is working fine but I am not seeing leading zeros in the csv file sent in the mail.These zeros were present when the .csv file was generated by informatica procees. Is there any... (11 Replies)
Discussion started by: karthik adiga
11 Replies

4. Shell Programming and Scripting

Clearing leading and trailing blanks from a string in C Shell

Does anyone know of a way with C Shell that will work on both Linux and Sun to clear all leading and trailing blanks from a previously specified string? I am using the following code to replace blanks with underscores: set Company = `echo $Company | sed 's/ /_/g but I don't want any... (1 Reply)
Discussion started by: phudgens
1 Replies

5. Shell Programming and Scripting

Help to move leading negative sign to trailing position

Hi All, I am having a file which contains negative numbers, wht i am doing is re-formattting the file(moving few columns and add few hard codings between), while reformatting i would want the negative numbers to have the sign as trailing rather than leading. Existing -2400.00 34 0.00... (11 Replies)
Discussion started by: selvankj
11 Replies

6. Shell Programming and Scripting

Remove trailing spaces from file

I'm currently writing my sql results to a file and they have trailing spaces after each field. I want to get rid of these spaces and I'm using this code: TVXTEMP=$(echo $TVXTEMP|sed -e 's/\ //g') It doesn't work though. I'm not familiar with sedscript, and the other codes I've found online... (6 Replies)
Discussion started by: avillanueva
6 Replies

7. Shell Programming and Scripting

Removing leading and trailing spaces only in PERL

Hi All, I have a file with the following contents with multiple lines 172445957| 000005911|8| 400 Peninsula Ave.#1551 | And,K |935172445957|000005911 607573888 |000098536 | 2|Ane, B |J |Ane |1868 |19861206|20090106|20071001 I want to trim the "leading and trailing spaces only" from... (2 Replies)
Discussion started by: kumar04
2 Replies

8. Shell Programming and Scripting

remove trailing and leading spaces using tr command

Dear All, can you please advice how do i remove trailing and leading spaces from a pipe-delimited file using "tr" command the below cmd, i tried removed all spaces tr -d ' '<s1.txt>s2.txt1 Many thx Suresh (5 Replies)
Discussion started by: sureshg_sampat
5 Replies

9. UNIX for Dummies Questions & Answers

Removing leading and trailing spaces of data between the tags in xml.

I am having xml document as below. <transactionid> 00 </transactionid> <tracknumber> 0 </tracknumber> <key> N/A </key> But the data contains leading and trailing spaces between the tags. Please let me know how can i remove these leading and trailing spaces between the tags.... (2 Replies)
Discussion started by: jhmr7
2 Replies

10. Shell Programming and Scripting

Leading and Trailing Spaces

Hi, how to i remove leading and trailing spaces from a line? the spaces can be behind or in front of any field or line example of a line in the input data: Amy Reds , 100 , /bin/sh how to i get it to be: Amy Read,100,/bin/sh i saw something on this on the Man pages for AWK... (7 Replies)
Discussion started by: sleepster
7 Replies
Login or Register to Ask a Question