LinuxSir.cn,穿越时空的Linuxsir!

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

怎样删除重复的strings?[基本解决]

[复制链接]
发表于 2004-5-3 01:48:49 | 显示全部楼层 |阅读模式
假设我有这样一个文字档:
  1. [color=blue]ba 罷[/color]
  2. ba 拔
  3. ba 芭
  4. ba 跋
  5. [color=orangered]ba 壩[/color]
  6. ba 耙
  7. ba 鮁
  8. ba ?
  9. [color=orangered]ba 壩[/color]
  10. ba 欛
  11. [color=blue]ba 罷[/color]
  12. ba 羓
  13. [color=green]chu 出[/color]
  14. [color=purple]chu 處[/color]
  15. chu 媰
  16. [color=green]chu 岀[/color]
  17. chu 幮
  18. chu ?N
  19. chu 除
  20. [color=purple]chu 處[/color]
复制代码

怎样用一行command把那些重复的strings删掉呢?
发表于 2004-5-3 02:17:44 | 显示全部楼层

  1. for line in $(cat file|cut -f2 -d' '|sort|uniq -u);do
  2.         grep $line file
  3. done|sort
复制代码
发表于 2004-5-3 02:34:47 | 显示全部楼层
awk '{ if ($0 in uniq){}else{print;uniq[$0]=1}}' datafile
发表于 2004-5-3 03:53:48 | 显示全部楼层
/home/javalee#cat file
ba 罷
ba 拔
ba 芭
ba 跋
ba 霸
ba 耙
ba 鮁
ba 霸
ba 疤
ba 罷
ba 罷
ba 羓
chu 出
chu 把


  1. /home/javalee#awk '!($2 in array){array[$2];print}' file
复制代码
/home/javalee#ksh sss
ba 罷
ba 拔
ba 芭
ba 跋
ba 霸
ba 耙
ba 鮁
ba 疤
ba 羓
chu 出
chu 把

ps:由于用鼠标粘贴后出现乱码,所以有的字是我自己改的~
发表于 2004-5-3 03:55:40 | 显示全部楼层
最初由 home_king 发表
awk '{ if ($0 in uniq){}else{print;uniq[$0]=1}}' datafile

king兄,你的测试好像不可以

  1. /home/javalee#awk '{ if ($0 in uniq){}else{print;uniq[$0]=1}}' file
  2. ba 罷
  3. ba 拔
  4. ba 芭
  5. ba 跋
  6. ba 霸
  7. ba 耙
  8. ba 鮁
  9. ba 霸
  10. ba 疤
  11. ba 罷
  12. ba 羓
  13. chu 出
  14. chu 把
复制代码
 楼主| 发表于 2004-5-3 04:21:07 | 显示全部楼层
这是我想要的结果:
  1. ba 罷
  2. ba 拔
  3. ba 芭
  4. ba 跋
  5. ba 壩
  6. ba 耙
  7. ba 鮁
  8. ba ?
  9. ba 欛
  10. ba 羓
  11. chu 出
  12. chu 處
  13. chu 媰
  14. chu 幮
  15. chu ?N
  16. chu 除
复制代码
发表于 2004-5-3 04:28:58 | 显示全部楼层
最初由 Hauser 发表
这是我想要的结果:
  1. ba 罷
  2. ba 拔
  3. ba 芭
  4. ba 跋
  5. ba 壩
  6. ba 耙
  7. ba 鮁
  8. ba ?
  9. ba 欛
  10. ba 羓
  11. chu 出
  12. chu 處
  13. chu 媰
  14. chu 幮
  15. chu ?N
  16. chu 除
复制代码

是呀,你看我贴你的文本后的样子?道理不是一样的嘛~~~~
你实验一下吧,应该管用的!
我说过我那个文本是我修改后的吗!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
 楼主| 发表于 2004-5-3 05:17:15 | 显示全部楼层
最初由 javalee 发表
  1. #awk '!($2 in array){array[$2];print}' file
复制代码

谢谢javalee,你这个管用了!:2cool
以后得常来这逛逛,学学unix的power tools!
发表于 2004-5-3 09:41:17 | 显示全部楼层
最初由 javalee 发表
king兄,你的测试好像不可以

  1. /home/javalee#awk '{ if ($0 in uniq){}else{print;uniq[$0]=1}}' file
  2. ba 罷
  3. ba 拔
  4. ba 芭
  5. ba 跋
  6. ba 霸
  7. ba 耙
  8. ba 鮁
  9. ba 霸
  10. ba 疤
  11. ba 罷
  12. ba 羓
  13. chu 出
  14. chu 把
复制代码

$2反而可以????
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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