简体   繁体   English

在每行.csv文件中更改日期格式

[英]Change a date format in .csv file in each row

I have a csv file like this: 我有这样的csv文件:

text;text;text;Date    
text;text;text;Date     
text;text;text;Date     

the date format is like this: 日期格式如下:

Mon 14 Nov 2016 13:07:30    

And I want change to this format (timestamp) in each row 14-11-2016 23:36:33 我想在每一行中更改此格式(时间戳) 14-11-2016 23:36:33

Using awk and date : 使用awk和date

$ awk -v OFS=\; -F\; -v qt="'" ' {
    str="date -d" qt $4 qt " " qt "+%Y-%m-%d %H:%M:%S" qt; 
    str | getline d; 
    print $1,$2,$3,d
}' file
text;text;text;2016-11-14 13:07:30

It takes the 4th field ( $4 ) delimited by ; 它需要用分隔的第四个字段( $4; ( -F\\; ), uses is as a parameter to system date command, reads its output to variable d and prints out three first fields and d . -F\\; )用作系统date命令的参数,将其输出读取到变量d并打印出第三个字段d

如果您的系统支持流程替换,则可以执行以下操作:

paste -d\; <(cut -d\; -f1-3 date.csv )  <(cut -d\; -f4 date.csv | date -f - '+%Y-%m-%d %H:%M:%S')

I did a quick little write-up for you on how to scrub CSV data in ruby 我为您快速整理了一些有关如何在ruby中清理CSV数据的文章

In short, given your input, you can scrub and convert the data with ruby like so: 简而言之,给定输入后,您可以使用ruby清理和转换数据,如下所示:

require 'date'                                                                                                                                                                                                                             

newFile = File.new("new.csv", "w+")                                                                                                                                                                                                        
oldFile = File.read("original.csv")                                                                                                                                                                                                        

oldFile.lines.each do |line|                                                                                                                                                                                                               
  lineArray = line.split(';')                                                                                                                                                                                                              
  formattedDate = DateTime.parse(lineArray[3]).strftime('%d-%m-%Y %H:%M:%S')                                                                                                                                                 
  lineArray[3] = formattedDate                                                                                                                                                                                                             

  newFile.puts "#{lineArray.join(';')}\n"                                                                                                                                                                                                  
end                                                                                                                                                                                                                                        

newFile.close  

All you need to do is save this script in your directory as parser.rb , make sure your original csv is saved as original.csv (or update the script accordingly) and then run ruby parser.rb in terminal. 您需要做的就是将此脚本保存在目录中作为parser.rb ,确保将原始csv保存为original.csv (或相应地更新脚本),然后在终端中运行ruby parser.rb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM