简体   繁体   English

awk:将日期字符串的格式设置为YYYYMMDD至YYYY-MM-DD

[英]awk: format date string from YYYYMMDD to YYYY-MM-DD

I have a CSV file which I parse using awk because I don't need all columns. 我有一个使用awk解析的CSV文件,因为我不需要所有列。 The problem I have is that one column is a date but in the format YYYYMMDD but I need it in YYYY-MM-DD and I don't know how to achieve that. 我的问题是,一列是日期,但格式为YYYYMMDD但我需要用YYYY-MM-DD ,但我不知道该如何实现。

I already tried with split($27, a) but it doesn't split it - so a[0] returns the whole string. 我已经尝试过split($27, a)但是它没有将其拆分-因此a[0]返回整个字符串。

You could use substr : 您可以使用substr

printf "%s-%s-%s", substr($27,0,4), substr($27,5,2), substr($27,7,2)

Assuming that the 27 th field was 20140318 , this would produce 2014-03-18 . 假设第27 字段是20140318 ,则将产生2014-03-18

Use your awk output as input to date -d , eg 使用您的awk输出作为date -d输入,例如

$ date -d 20140918 +'%Y-%m-%d'
2014-09-18

暂无
暂无

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

相关问题 使用 shell 脚本将文件中的日期格式从 dd/mm/yyyy 更改为 yyyy-mm-dd - Change date format from dd/mm/yyyy to yyyy-mm-dd in a file using shell scripting bash和awk脚本以YYYY-MM-DD格式获取上个月的日期 - bash and awk script get to previous month date in format YYYY-MM-DD 将 Linux 日期转换为 yyyy-MM-dd'T'HH:mm:ss'Z' 格式 - convert Linux date to yyyy-MM-dd'T'HH:mm:ss'Z' format 如何编辑 Linux 中文本文件中的行 - 将日期格式化为 YYYY-MM-DD,然后按时间段将行格式化为 grep - How to edit the lines in text file in Linux - format the date to YYYY-MM-DD and then grep the line by time period 严格在Linux上解析YYYY-MM-DD日期 - Parsing a YYYY-MM-DD date strictly on Linux 当日期编码为diry作为yyyy-mm-dd时,如何查找早于日期的目录 - How to find directories older than date when the date is encoded to dirname as yyyy-mm-dd bash中以dd / mm / yyyy格式表示的特定日期的天数总和 - Sum number of days to specific date in dd/mm/yyyy format in bash 如何使用同一命令在Linux和Powershell中以“ YYYY-MM-DD”格式回显日期时间? - How echo a datetime in “YYYY-MM-DD” format in linux and powershell with a same command? 如何使用 sed 命令将日期从 MM/DD/YYYY 更改为 MM-DD-YYYY - how to change date from MM/DD/YYYY to MM-DD-YYYY using sed command 将日期从ISO时间格式转换为yyyy / mm / dd.hh:mm:ss - Convert dates from ISO time format to yyyy/mm/dd.hh:mm:ss
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM