LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1819|回复: 9

怎样合并相同行?

[复制链接]
发表于 2007-5-17 14:21:12 | 显示全部楼层 |阅读模式
例如:
a
b
b
b
c

合并相邻的相同行为一行:
a
b
c

原先以为uniq可以,但uniq把相同行都删掉了。
 楼主| 发表于 2007-5-17 14:38:59 | 显示全部楼层
找到个好方法:

awk '!a[$0]++' file
回复 支持 反对

使用道具 举报

发表于 2007-5-17 15:27:24 | 显示全部楼层
sort -u也可以的吧
回复 支持 反对

使用道具 举报

发表于 2007-5-17 15:53:06 | 显示全部楼层
我这的uniq正常(版本是5.97),sort会排序的楼主应该不用排序的吧。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-5-17 15:57:30 | 显示全部楼层
对,正是因为不要排序,所以才不用sort。

uniq会把相同的行全部删掉,而我是想只留一行,不知ls的uniq有这种功能?我用的是5.93的版本。
回复 支持 反对

使用道具 举报

发表于 2007-5-17 21:21:14 | 显示全部楼层
如果相同的行不在一起, uniq不起作用.
回复 支持 反对

使用道具 举报

发表于 2007-5-18 19:46:18 | 显示全部楼层
相同的行不在一起,awk的方法也不成。
回复 支持 反对

使用道具 举报

发表于 2007-5-27 02:12:14 | 显示全部楼层
.........
回复 支持 反对

使用道具 举报

发表于 2007-10-11 17:17:10 | 显示全部楼层

用Shell脚本

uniq  1.log -c
你好好看看吧
回复 支持 反对

使用道具 举报

发表于 2007-10-12 07:50:55 | 显示全部楼层

  1. uniq 文件
复制代码

uniq --version
uniq (GNU coreutils) 5.97
Copyright (C) 2006 Free Software Foundation, Inc.
这是自由软件。您可以按照 GNU GPL 协议 <http://www.gnu.org/licenses/gpl.html> 的条款再发布此软件的副本,但我们无法保证相关法律不对这一情形进行限制。

由 Richard Stallman 和 David MacKenzie 编写。


  1. awk 'BEGIN{oldline="\n"}{if($0!=oldline)print $0;oldline=$0}'  文件
复制代码

awk --version
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.


  1. (oldline=$'\n\n'; while read line;do [ "$oldline" != "$line" ] && echo $line ; oldline="$line";done) <文件
复制代码

bash --version
GNU bash, version 3.1.17(1)-release (i486-pc-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表