免费注册 查看新帖 |

Chinaunix

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

如何利用awk将输出的的顺序进行置换 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-15 19:17 |只看该作者 |倒序浏览
需要处理的文件如下
awk -F "," '{print $0}' test.txt
0633,010083002028,yourname,9456.41,200906,311111160000

希望输出列的顺序为 $1,$2,$3,$6,$5,$4  
执行如下命令没有达到预期效果
awk -F "," '{OFS=",";print $1,$2,$3,$6,$5,$4}'  test.txt
,200906,9456.4128,yourname,311111160000

请指教错误在哪里,应该如何实现

论坛徽章:
0
2 [报告]
发表于 2009-08-15 19:55 |只看该作者
我这试了没问题。是不是你的不是gawk。

如果不是gawk,还是使用最标准的写法
awk 'BEGIN{FS=",";OFS=","}{print $1, $2, $3, $6, $5, $4}' test.txt
试试。

因为 -F "," 我猜测,是不是会把F后面的空格解释为FS。

论坛徽章:
0
3 [报告]
发表于 2009-08-15 20:03 |只看该作者
百思不得其解,看样子不是分割符的问题
awk 'BEGIN{FS=",";OFS=","}{print $1,$2,$3,$6,$5,$4}' test.txt
,200906,9456.4128,yourname,311111160000

怎么知道awk的版本?

论坛徽章:
0
4 [报告]
发表于 2009-08-15 20:16 |只看该作者
awk -F "," '{OFS=",";print $1,$2,$3,$6,$5,$4}'  test.txt
awk 'BEGIN{FS=",";OFS=","}{print $1, $2, $3, $6, $5, $4}' test.txt

都没有问题啊。。。

$awk --v
GNU Awk 3.1.5
Copyright (C) 1989, 1991-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

论坛徽章:
0
5 [报告]
发表于 2009-08-15 20:22 |只看该作者
我是装的
Red Hat Enterprise Linux AS release 4 (Nahant Update 7)
默认的awk版本是
awk --v
GNU Awk 3.1.3

论坛徽章:
0
6 [报告]
发表于 2009-08-15 20:32 |只看该作者
应该不是awk本身的问题。

要不是显示终端的问题。

论坛徽章:
0
7 [报告]
发表于 2009-08-15 20:43 |只看该作者
找到原因了,原来是第六列换行符被带到了现在第4列的位置
0633,010083002028,yourname,311111160000^M,200906,9456.41

请问如何避免出现这个问题呢

论坛徽章:
0
8 [报告]
发表于 2009-08-15 20:47 |只看该作者
dos2unix test.txt

awk 'BEGIN{FS=",";OFS=","}{print $1,$2,$3,$6,$5,$4}' test.txt

论坛徽章:
0
9 [报告]
发表于 2009-08-15 20:52 |只看该作者
结案,多谢yy

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
10 [报告]
发表于 2009-08-15 21:46 |只看该作者

回复 #3 jiajuzh 的帖子

awk --version

是什么环境?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP