将月份名称更改为sed awk或批处理中特定行的月份编号

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将月份名称更改为sed awk或批处理中特定行的月份编号相关的知识,希望对你有一定的参考价值。

我有一个文件,其中包含测量的观察时间(时间和日期)。

输入文件:

TimeStamp=Wednesday,February 21,2018 4:33:10 PM   
1,16.00,17.11,703.41,257.462,12.3,13.8   
2,17.11,18.30,753.61,308.084,13.8,15.3    
3,18.30,19.58,733.57,335.605,15.3,18.0   
TimeStamp=Wednesday,February 21,2018 4:35:54 PM  
1,16.00,17.11,638.19,233.251,12.3,13.5  
2,17.11,18.30,606.43,247.558,13.5,15.5  
3,18.30,19.58,708.14,323.502,15.5,18.2  
TimeStamp=Wednesday,February 21,2018 4:38:37 PM  
1,16.00,17.11,543.10,201.339,12.5,13.5  
2,17.11,18.30,542.06,224.449,13.5,15.3  
3,18.30,19.58,693.38,321.293,15.3,17.8  

我想将月份名称转换为特定行的月号,其中包含“TimeStamps =”。此外,我还想将时间从12小时格式更改为24小时格式。

输出文件:

TimeStamp=Wednesday,02 21,2018 16:33:10  
1,16.00,17.11,703.41,257.462,12.3,13.8   
2,17.11,18.30,753.61,308.084,13.8,15.3    
3,18.30,19.58,733.57,335.605,15.3,18.0   
TimeStamp=Wednesday,02 21,2018 16:35:54  
1,16.00,17.11,638.19,233.251,12.3,13.5  
2,17.11,18.30,606.43,247.558,13.5,15.5  
3,18.30,19.58,708.14,323.502,15.5,18.2  
TimeStamp=Wednesday,02 21,2018 16:38:37  
1,16.00,17.11,543.10,201.339,12.5,13.5  
2,17.11,18.30,542.06,224.449,13.5,15.3  
3,18.30,19.58,693.38,321.293,15.3,17.8 

请帮忙。

先感谢您。

答案

在sed脚本之后,可能是一个开始,可以根据输入进行改进或修复:

/^TimeStamp=/ {
 s/January/01/
 s/February/02/
 s/March/03/
 s/April/04/
 s/May/05/
 s/June/06/
 s/July/07/
 s/August/08/
 s/September/09/
 s/October/10/
 s/November/11/
 s/December/12/
 / PM *$/ {
  s/ PM *$//
  s/ 1:/ 13:/
  s/ 2:/ 14:/
  s/ 3:/ 15:/
  s/ 4:/ 16:/
  s/ 5:/ 17:/
  s/ 6:/ 18:/
  s/ 7:/ 19:/
  s/ 8:/ 20:/
  s/ 9:/ 21:/
  s/ 10:/ 22:/
  s/ 11:/ 23:/
 }
 / AM *$/ {
  s/ AM *$//
  s/ 12:/ 0:/
 }
}

sed命令

sed -f script_name < input_file > output_file
另一答案

使用awk

$ awk -F "[, ]" ' /Time/{"date --date=""$2" "$3" "$4"" +%m" | getline m; "date --date=""$5" "$6"" +%T" | getline t; printf("%s,%02d %d,%d %s
",$1,m,$3,$4,t); next}1' file
TimeStamp=Wednesday,02 21,2018 16:33:10
1,16.00,17.11,703.41,257.462,12.3,13.8
2,17.11,18.30,753.61,308.084,13.8,15.3
3,18.30,19.58,733.57,335.605,15.3,18.0
TimeStamp=Wednesday,02 21,2018 16:35:54
1,16.00,17.11,638.19,233.251,12.3,13.5
2,17.11,18.30,606.43,247.558,13.5,15.5
3,18.30,19.58,708.14,323.502,15.5,18.2
TimeStamp=Wednesday,02 21,2018 16:38:37
1,16.00,17.11,543.10,201.339,12.5,13.5
2,17.11,18.30,542.06,224.449,13.5,15.3
3,18.30,19.58,693.38,321.293,15.3,17.8

我们在qazxsw poi中调用qazxsw poi

date:在日期命令和月份整数结果中传递必填字段将在awk中存储

"date --date=""$2" "$3" "$4"" +%m" | getline m;:类似的m将有24小时格式的时间

另一答案
"date --date=""$5" "$6"" +%T" | getline t;
另一答案

扩展的t解决方案:

@echo off
setlocal EnableDelayedExpansion

set /A i=100, AM=0, PM=12
for %%a in (January February March April May June July August September October November December) do (
   set /A i+=1
   set "month[%%a]=!i:~1!"
)

(for /F "delims=" %%l in (input.txt) do (
   set "line=%%l"
   if "!line:~0,4!" equ "Time" (
      for /F "tokens=1-8 delims=,: " %%a in ("%%l") do (
         set /A hour=%%e+%%h
         set "line=%%a,!month[%%b]! %%c,%%d !hour!:%%f:%%g"
      )
   )
   echo !line!
)) > output.txt

输出:

awk
另一答案

awk 'BEGIN{ split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", month); for (i in month) months[month[i]] = sprintf("%02d", i); FS = OFS = "," } /TimeStamp/{ sub(/^[^[:space:]]+/, months[substr($2, 1, 3)], $2); split($3, t, " "); h=sprintf("%02d", substr(t[2], 1, length(t[2])==8? 2 : 1) + (t[3]=="PM"? 12 : 0)); if (h == 24) h="00"; sub(/^[0-9]+/, h, t[2]); $3 = t[1]" "t[2] }1' file 救援! (使用系统日期)

TimeStamp=Wednesday,02 21,2018 16:33:10
1,16.00,17.11,703.41,257.462,12.3,13.8   
2,17.11,18.30,753.61,308.084,13.8,15.3    
3,18.30,19.58,733.57,335.605,15.3,18.0   
TimeStamp=Wednesday,02 21,2018 16:35:54
1,16.00,17.11,638.19,233.251,12.3,13.5  
2,17.11,18.30,606.43,247.558,13.5,15.5  
3,18.30,19.58,708.14,323.502,15.5,18.2  
TimeStamp=Wednesday,02 21,2018 16:38:37
1,16.00,17.11,543.10,201.339,12.5,13.5  
2,17.11,18.30,542.06,224.449,13.5,15.3  
3,18.30,19.58,693.38,321.293,15.3,17.8

以上是关于将月份名称更改为sed awk或批处理中特定行的月份编号的主要内容,如果未能解决你的问题,请参考以下文章

linux文本处理绝世好剑--awk

awk编辑器

awk

jQuery UI datepicker:如何将下拉列表中的月份名称从短名称更改为长名称?

将月份编号更改为月份名称

使用 Mongodb 聚合如何将数字更改为月份名称