用awk给某一列的时间+8小时,date的%d和sprintf的%d冲突怎么办

kailun0315 2016-10-11 05:26:52
文本中时间是格林威治时间,想转成北京时间。
MF1007,2016-9-10 11:00:00,2016-9-10 17:04:00
ET607,2016-9-10 16:30:00,2016-9-10 17:01:00
FX5194,2016-9-10 16:15:00,2016-9-10 16:56:00
AF4403,2016-9-10 16:20:00,2016-9-10 16:54:00

想用这个命令,可是%d的定义冲突了,怎么办。
awk -F"," '{sprintf("date -d \"%s 8hours\" \"+%Y-%m-%d %H:%M:%S\"", $2)| getline d; print d}' a.txt

或者有么有其他方法可以转换时间。
...全文
553 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ipqtjmqj 2016-10-13
  • 打赏
  • 举报
回复
%%d转义成%d
ipqtjmqj 2016-10-12
  • 打赏
  • 举报
回复

#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
void fun(char const *path, char seperator, int no);
int main(int n, char **v)
{
	if (n != 4)
	{
		puts("usage: <filepath> <column seperator>(one character) <column number>(begin from 1) ");
	}
	else
	{
		fun(v[1], v[2][0], atoi(v[3]));
	}
	return 0;
}
void fun_line(std::string & , char, int);
void fun(char const *path, char seperator, int no)
{
	std::vector<std::string> file_content;

	std::ifstream ifile(path);	
	for (std::string tmp; std::getline(ifile, tmp); fun_line(tmp, seperator, no), file_content.push_back(tmp));
	ifile.close();
	
	std::ofstream ofile(path);
	for (int i = 0; i != file_content.size(); ofile << file_content[i++] << std::endl);
	ofile.close();
}
void fun_elem(std::string &elem);
void fun_line(std::string &line, char sep, int no)
{
	std::vector<std::string> line_content;
	
	std::istringstream iss(line);
	for (std::string tmp; std::getline(iss, tmp, sep); line_content.push_back(tmp));
	fun_elem(line_content[no - 1]);

	line.clear();
	for (int i = 0; i != line_content.size(); line += line_content[i++])
	{
		if (i != 0) line.push_back(sep);
	}
}
#include <time.h>
void fun_elem(std::string &elem)
{
	char const *fmt = "%d-%d-%d %02d:%02d:%02d";
	tm t;
	sscanf(elem.c_str(), fmt, 
		&t.tm_year, &t.tm_mon, &t.tm_mday, 
		&t.tm_hour, &t.tm_min, &t.tm_sec);
	t.tm_year -= 1900;
	t.tm_mon -= 1;
	time_t tt = mktime(&t);
	tt += 8 * 60 * 60;
	tm *pt = localtime(&tt);
	char buf[64];
	sprintf(buf, fmt, 
		pt->tm_year + 1900, pt->tm_mon + 1, pt->tm_mday,
		pt->tm_hour, pt->tm_min, pt->tm_sec);
	elem = buf;
}
ipqtjmqj 2016-10-12
  • 打赏
  • 举报
回复
我只会c++, 先getline, 再sscanf, 修改后sprintf
Re:《文本处理 awk sed grep ”三剑客”》 ============================================================== 本人在教学和实战过程中发现,即便是有一定运维经验的人,可能已经能够搭建一定复杂度的Linux架构,但是在来来回回的具体操作中,还是体现出CLI(命令界面)功底不够扎实,甚至操作的非常‘拙’、处处露‘怯’。 对一个士兵来说,枪就是他的武器,对于一个程序员来说,各种library(工具库)就是他的武器;而对于Linux运维人员来说,无疑命令行工具CLI(命令界面)就是他们的武器;高手和小白之间的差距往往就体现在对于这些“武器”的掌握和熟练程度上。有时候一个参数就能够解决的事情,小白们可能要写一个复杂的Shell脚本才能搞定,这就是对CLI(命令界面)没有理解参悟透彻导致。 研磨每一个命令就是擦拭手中的作战武器,平时不保养不理解,等到作战的时候,一定不能够将手中的武器发挥到极致,所以我们要平心、静气和专注,甘坐冷板凳一段时间,才能练就一身非凡的内功! 本教程从实战出发,结合当下流行或最新的Linux(v6/7/8 版本)同时演示,将命令行结合到解决企业实战问题中来,体现出教学注重实战的务实精神,希望从事或未来从事运维的同学,能够认真仔细的学完Linux核心命令的整套课程。 本课程系列将逐步推出,看看我教学的进度和您学习的步伐,孰占鳌头! 注:关于教学环境搭建,可以参考本人其它课程系列,本教学中就不再赘述! 《参透 VMware 桌面级虚拟化》 《在虚拟机中安装模版机(包括应用软件等)》 《SecureCRT 连接 GNS3/Linux 的安全精密工具》---------------------------------------         

23,110

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧