Unix:删除重复的行而不进行排序

编程入门 行业动态 更新时间:2024-10-23 13:30:57
本文介绍了Unix:删除重复的行而不进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在Python中有一个实用程序脚本: $ b $ pre $ code $#/ usr / bin / env python import sys unique_lines = [] duplicate_lines = [] 用于sys.stdin中的行: if line中的unique_lines: duplicate_lines.append(line)否则: unique_lines.append(line) sys.stdout.write(line)#可选地做一些与duplicate_lines

这个简单的功能(uniq,不需要先排序,稳定的排序)必须作为一个简单的UNIX工具使用,不是吗?也许在一个管道中的过滤器的组合?

询问的理由:需要这个功能的系统上,我不能从任何地方执行python UNIX Bash脚本博客 .htmlrel =noreferrer>建议:

awk'!x [$ 0] ++'

这个命令只是告诉awk要打印哪些行。变量 $ 0 包含一行的全部内容,方括号是数组访问。因此,对于文件的每一行,数组 x 的节点增加,如果该节点的内容不是(! )以前设置。

I have a utility script in Python:

#!/usr/bin/env python import sys unique_lines = [] duplicate_lines = [] for line in sys.stdin: if line in unique_lines: duplicate_lines.append(line) else: unique_lines.append(line) sys.stdout.write(line) # optionally do something with duplicate_lines

This simple functionality (uniq without needing to sort first, stable ordering) must be available as a simple UNIX utility, mustn't it? Maybe a combination of filters in a pipe?

Reason for asking: needing this functionality on a system on which I cannot execute python from anywhere

解决方案

The UNIX Bash Scripting blog suggests:

awk '!x[$0]++'

This command is simply telling awk which lines to print. The variable $0 holds the entire contents of a line and square brackets are array access. So, for each line of the file, the node of the array x is incremented and the line printed if the content of that node was not (!) previously set.

更多推荐

Unix:删除重复的行而不进行排序

本文发布于:2023-11-29 00:52:43,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不   Unix

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!