用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

或者有么有其他方法可以转换时间。
...全文
556 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

23,124

社区成员

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

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