AWK - 在特定的列值分割文件 [英] AWK - Split file by value in specific column

查看:307
本文介绍了AWK - 在特定的列值分割文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下awk脚本(通过Armali本网站提供的),基本上条按日期(月/年)制表符分隔的文件,并将其保存为yyyymmm。我现在有另外一个附加条件通过该文件应该被拆分。它应该按月/年,列3将文件保存为yyyymmm_Col3Uniquevalue。被拆分并且还通过独特的价值

当前脚本

  awk的NR大于1 {斯普利特($ 2,日期,\\/ \\);打印>日[3]的strftime(\\%% b.txt \\(日[2] -1)* 31 * 24 * 60 * 60)}input.txt中

数据格式:


国家日期类型
香港影视31/01/2012
日本14/01/2012 preSS
日本05/01/2012电视
日本16/02/2013 preSS
日本15/02/2013电视

输出4 TXT文件:


2012Jan_ preSS - 包含记录2
2012Jan_Television - 包含记录1,3
2013Feb_ preSS - 包含记录4
2013Feb_Television - 包含5纪录


解决方案

使用制表符分隔...

 的awk -F \\ t的NR大于1 {斯普利特($ 2,日期,\\/ \\);打印>日[3]的strftime(\\%% B_ \\ $ 3 \\。TXT \\(日期[2] -1)* 31 * 24 * 60 * 60)}input.txt中

$ 3 必须从引用的格式字符串排除在外。

如果日期字段 $ 2 包含空格也的时候,受空间以及由拆分后/继续得到当年日期[3]

 的awk -F \\ t的NR大于1 {斯普利特($ 2,日期,\\[/] \\);打印>日[3]的strftime(\\%% B_ \\$ 3 \\。TXT \\(日期[2] -1)* 31 * 24 * 60 * 60)}input.txt中

I have the following AWK script (provided by Armali on this site) which basically strips a tab delimited file by date(Month/year) and saves it as yyyymmm. I now have another additional condition by which the file should be split. It should be split by Month/year and also by the unique value in Column 3. Save the file as yyyymmm_Col3Uniquevalue.

The current script is

awk "NR>1{split($2,date,\"/\");print>date[3]strftime(\"%%b.txt\",(date[2]-1)*31*24*60*60)}" input.txt 

Data Format:

Country Date    Type
HongKong    31/01/2012  Television
Japan   14/01/2012  Press
Japan   05/01/2012  Television
Japan   16/02/2013  Press
Japan   15/02/2013  Television

Output will be 4 txt files:

2012Jan_Press - Containing record 2
2012Jan_Television - Containing record 1,3
2013Feb_Press - Containing record 4
2013Feb_Television - Containing record 5

解决方案

With TAB separated fields...:

awk -F\t "NR>1{split($2,date,\"/\");print>date[3]strftime(\"%%b_\"$3\".txt\",(date[2]-1)*31*24*60*60)}" input.txt

$3 had to be excluded from the quoted format string.

If the date field $2 contains after a space also the time, split by space as well as by "/" to keep getting the year in date[3]:

awk -F\t "NR>1{split($2,date,\"[/ ]\");print>date[3]strftime(\"%%b_\"$3\".txt\",(date[2]-1)*31*24*60*60)}" input.txt

这篇关于AWK - 在特定的列值分割文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆