免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2098 | 回复: 9
打印 上一主题 下一主题

[学习共享] 如何输出多个文件中的指定几个字段? [复制链接]

论坛徽章:
29
程序设计版块每日发帖之星
日期:2016-02-29 06:20:0015-16赛季CBA联赛之天津
日期:2016-08-10 10:33:1115-16赛季CBA联赛之深圳
日期:2016-08-17 15:07:2015-16赛季CBA联赛之佛山
日期:2016-11-07 11:33:5015-16赛季CBA联赛之广夏
日期:2016-11-15 09:13:31CU十四周年纪念徽章
日期:2016-11-24 14:12:25极客徽章
日期:2016-12-07 14:03:4015-16赛季CBA联赛之深圳
日期:2016-12-07 17:15:2715-16赛季CBA联赛之北京
日期:2016-12-22 09:30:0115-16赛季CBA联赛之深圳
日期:2016-12-22 10:49:2115-16赛季CBA联赛之山西
日期:2017-02-10 09:05:3215-16赛季CBA联赛之同曦
日期:2017-02-27 14:19:08
1 [报告]
发表于 2016-03-16 09:51 |只看该作者
回复 1# johnson0418
  1. awk -vRS="[()\n]+" 'FNR==7||FNR==5||FNR==21||FNR==81||FNR==25||FNR==31||FNR==35{gsub(/[[:space:]]+$/,"");split($0,s,/=/);a[s[1]]=a[s[1]]?a[s[1]]"|"s[2]:s[2]}END{for(i in a)print i"|"a[i]}' *.DAT | awk -F\| '{for(i=1;i<=NF;++i)b[NR,i]=$i}END{for(i=1;i<=NF;++i){for(j=1;j<=NR;++j)printf("%-20s", b[j,i]);print ""}}'
复制代码

论坛徽章:
54
2015亚冠之德黑兰石油
日期:2015-07-07 13:00:1615-16赛季CBA联赛之深圳
日期:2016-03-31 09:03:5415-16赛季CBA联赛之辽宁
日期:2016-05-09 20:38:15程序设计版块每日发帖之星
日期:2016-05-12 06:20:0015-16赛季CBA联赛之四川
日期:2016-05-13 15:19:4715-16赛季CBA联赛之福建
日期:2016-05-15 20:24:34每日论坛发贴之星
日期:2016-05-16 06:20:0015-16赛季CBA联赛之吉林
日期:2016-05-26 11:49:4715-16赛季CBA联赛之广东
日期:2016-05-26 13:49:18极客徽章
日期:2016-12-07 14:05:2315-16赛季CBA联赛之广夏
日期:2016-12-20 17:33:532017金鸡报晓
日期:2017-01-10 15:19:56
2 [报告]
发表于 2016-03-16 10:50 |只看该作者
  1. awk -vRS= -F=' *\\(' -vs='ChargedNumber       CalledNumber        CallType            StartDateTime       EndDateTime         Duration            TrunkGroupIdentity' 'BEGIN{p=split(s,t,"  *");print s}a!=FILENAME{if(a)print m;a=FILENAME}{for(i=1;i<=p;i++){if(match($0,t[i]" = ([^(\n]+)",n)){sub(" *$","",n[1]);printf "%-20 s",n[1]}}}' *
复制代码

论坛徽章:
4
每日论坛发贴之星
日期:2015-12-02 06:20:00程序设计版块每日发帖之星
日期:2015-12-12 06:20:00程序设计版块每日发帖之星
日期:2015-12-15 06:20:00程序设计版块每日发帖之星
日期:2015-12-19 06:20:00
3 [报告]
发表于 2016-03-16 21:21 |只看该作者
本帖最后由 johnson0418 于 2016-03-16 22:51 编辑

回复 4# haooooaaa

users>more dd
awk -vRS= -F=' *\\(' -vs='ChargedNumber       CalledNumber        CallType            StartDateTime       EndDateTi
me         Duration            TrunkGroupIdentity' 'BEGIN{p=split(s,t,"  *");print s}a!=FILENAME{if(a)print m
;a=FILENAME}{for(i=1;i<=p;i++){if(match($0,t" = ([^(\n]+)",n)){sub(" *$","",n[1]);printf "%-20 s",n[1]}}}' *

users>./dd
awk: cmd. line:1: fatal: match() cannot have 3 arguments


    awk: option requires an argument -- W
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
        awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options:                GNU long options:
        -f progfile                --file=progfile
        -F fs                        --field-separator=fs
        -v var=val                --assign=var=val
        -m[fr] val
        -W compat                --compat
        -W copyleft                --copyleft
        -W copyright                --copyright
        -W help                        --help
        -W lint                        --lint
        -W lint-old                --lint-old
        -W posix                --posix
        -W re-interval                --re-interval
        -W source=program-text        --source=program-text
        -W traditional                --traditional
        -W usage                --usage
        -W version                --version

To report bugs, see node `Bugs' in `gawk.info', which
is section `Reporting Problems and Bugs' in the
printed version.
(1)xa000000> awk --version
GNU Awk 3.0.6
Copyright (C) 1989, 1991-2000 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

论坛徽章:
4
每日论坛发贴之星
日期:2015-12-02 06:20:00程序设计版块每日发帖之星
日期:2015-12-12 06:20:00程序设计版块每日发帖之星
日期:2015-12-15 06:20:00程序设计版块每日发帖之星
日期:2015-12-19 06:20:00
4 [报告]
发表于 2016-03-16 21:22 |只看该作者
本帖最后由 johnson0418 于 2016-03-16 22:52 编辑

回复 2# elu_ligao


users>more ff
awk -vRS="[()\n]+" 'FNR==7||FNR==5||FNR==21||FNR==81||FNR==25||FNR==31||FNR==35{gsub(/[[:space:]]+$/,"");split($0,s
,/=/);a[s[1]]=a[s[1]]?a[s[1]]"|"s[2]:s[2]}END{for(i in a)print i"|"a}' *.DAT | awk -F\| '{for(i=1;i<=NF;++i)b[
NR,i]=$i}END{for(i=1;i<=NF;++i){for(j=1;j<=NR;++j)printf("%-20s", b[j,i]);print ""}}'

users> ./ff
09                  39                  01                  11                  02                  14                  16        

输出的是标题,非内容!!

论坛徽章:
29
程序设计版块每日发帖之星
日期:2016-02-29 06:20:0015-16赛季CBA联赛之天津
日期:2016-08-10 10:33:1115-16赛季CBA联赛之深圳
日期:2016-08-17 15:07:2015-16赛季CBA联赛之佛山
日期:2016-11-07 11:33:5015-16赛季CBA联赛之广夏
日期:2016-11-15 09:13:31CU十四周年纪念徽章
日期:2016-11-24 14:12:25极客徽章
日期:2016-12-07 14:03:4015-16赛季CBA联赛之深圳
日期:2016-12-07 17:15:2715-16赛季CBA联赛之北京
日期:2016-12-22 09:30:0115-16赛季CBA联赛之深圳
日期:2016-12-22 10:49:2115-16赛季CBA联赛之山西
日期:2017-02-10 09:05:3215-16赛季CBA联赛之同曦
日期:2017-02-27 14:19:08
5 [报告]
发表于 2016-03-17 08:55 |只看该作者
你的 awk 版本不同, 3楼4楼的都能输出你要的结果的, 当然我那个也是可以的, 但3 4 楼的更通用, 按你关键字来的, 你文件里面只要有这些关键字的配置都能取出来, 把你awk 版本贴出来看看吧

论坛徽章:
4
每日论坛发贴之星
日期:2015-12-02 06:20:00程序设计版块每日发帖之星
日期:2015-12-12 06:20:00程序设计版块每日发帖之星
日期:2015-12-15 06:20:00程序设计版块每日发帖之星
日期:2015-12-19 06:20:00
6 [报告]
发表于 2016-03-17 15:43 |只看该作者
回复 8# elu_ligao

你的方法我可以执行,但就是输出的内容不对。麻烦你在看看是不是有的字段写的不对?
3楼的太复杂了,我搞不明白。
4楼的在我的设备里不能执行,但在redhat 9里是可以的。但我需要在我的设备里执行,且不能升级AWK。
users> awk --version
GNU Awk 3.0.6
Copyright (C) 1989, 1991-2000 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP