Linux 如何找到存储在文件“data.txt”中的特定文本并且它只出现一次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12782827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:32:33  来源:igfitidea点击:

How to find the particular text stored in the file "data.txt" and it occurs only once

linuxsearch

提问by user1565960

The line I seek is stored in the file data.txt and is the only line of text that occurs only once.

我查找的行存储在文件 data.txt 中,并且是唯一仅出现一次的文本行。

How do I go about finding that particular line using linux?

如何使用 linux 查找该特定行?

回答by daemonofchaos

The following will get you what you need.

以下内容将为您提供所需的内容。

grep 'password' data.txt 

回答by Demagog

Add more information to you post. How data.txtlook like? Like this:

为您的帖子添加更多信息。长data.txt得怎么样?像这样:

11111111
11111111
pass1111
11111111

Or like this

或者像这样

afawfdgd
password
somethin
gelse...

And, do you know the password is in file or you search for not repeat string.

而且,您是否知道密码在文件中,或者您搜索的是不重复的字符串。

If you know password, use something like this

如果您知道密码,请使用类似的方法

cat data.txt | grep 'password'

cat data.txt | grep 'password'

If you don`t know the password and this password is only unique line in file you must create a script. For example in Python

如果您不知道密码并且此密码只是文件中唯一的一行,则您必须创建一个脚本。例如在 Python 中

file = open("data.txt","r")
f = file.read()
for line in f: 
   if 'pass' in line:
        print pass

Of course replace pass with something else. For example some slice from line.

当然用别的东西代替pass。例如从线一些切片。

回答by BostonGeorge

This is a little bit old, but I think you are looking for this...

这有点旧,但我认为你正在寻找这个......

cat data.txt | sort | uniq -u

This will show the unique values that only occur once in the file. I assume you are familiar with "over the wire" if you are asking?? If so, this is what you are looking for.

这将显示在文件中仅出现一次的唯一值。如果您要问,我假设您熟悉“在线”?如果是这样,这就是您要寻找的。

回答by Paul Tibbetts

To provide some context (I need more rep to comment) this is a question that features in an online "wargame" called Banditthat involves using the command line to discover passwords on an online Linux server to advance up the levels.

为了提供一些上下文(我需要更多代表发表评论),这是一个名为 Bandit 的在线“战争游戏”中的一个问题,该问题涉及使用命令行来发现在线 Linux 服务器上的密码以提高级别。

For those who would like to see data.txt in full I've Pastebin'd it herehowever it looks like this:

对于那些想要完整查看 data.txt 的人,我已将其粘贴到此处,但它看起来像这样:

NN4e37KW2tkIb3dC9ZHyOPdq1FqZwq9h
jpEYciZvDIs6MLPhYoOGWQHNIoQZzE5q
3rpovhi1CyT7RUTunW30goGek5Q5Fu66
JOaWd4uAPii4Jc19AP2McmBNRzBYDAkO
JOaWd4uAPii4Jc19AP2McmBNRzBYDAkO
9WV67QT4uZZK7JHwmOH0jnhurJMwoGZU
a2GjmWtTe3tTM0ARl7TQwraPGXgfkH4f
7yJ8imXc7NNiovDuAl1ZC6xb0O0mMBx1
UsvVyFSfZZWbi6wgC7dAFyFuR6jQQUhR
FcOJhZkHlnwqcD8QbvjRyn886rCrnWZ7
E3ugYDa6Wh2y8C8xQev7vOS8O3OgG1Hw
E3ugYDa6Wh2y8C8xQev7vOS8O3OgG1Hw
ME7nnzbId4W3dajsl6Xtviyl5uhmMenv
J5lN3Qe4s7ktiwvcCj9ZHWrAJcUWEhUq
aouHvjzagN8QT2BCMB6e9rlN4ffqZ0Qq
ZRF5dlSuwuVV9TLhHKvPvRDrQ2L5ODfD
9ZjR3NTHue4YR6n4DgG5e0qMQcJjTaiM
QT8Bw9ofH4x3MeRvYAVbYvV1e1zq3Xim
i6A6TL6nqvjCAPvOdXZWjlYgyvqxmB7k
tx7tQ6kgeJnC446CHbiJY7fyRwrwuhrs

One way to do it is to use:

一种方法是使用:

sort data.txt | uniq -u

The sortcommand is like cat in that it displays the contents of the file however it sorts the file lexicographicallyby lines (it reorders them alphabetically so that matching ones are together).

sort命令类似于 cat ,因为它显示文件的内容,但是它按行按字典顺序对文件进行排序(它按字母顺序对它们重新排序,以便匹配的内容在一起)。

The |is a pipe that redirects the output from one command into another.

|是一个将一个命令的输出重定向到另一个命令的管道。

The uniqcommand reports or omits repeated lines and by passing it the -uargument we tell it to report only unique lines.

uniq命令报告或省略重复的行,并通过向它传递-u参数,我们告诉它只报告唯一的行。

Used together like this, the command will sort data.txt lexicographically by each line, find the unique line and print it back in the terminal for you.

像这样一起使用,该命令将按每行字典序对 data.txt 进行排序,找到唯一的行并将其打印回终端。

回答by Mladen.Trampic

sort -u data.txt | while read line; do if [ $(grep -c $line data.txt) == 1 ] ;then echo $line; fi; done 

was mine solution, until I saw here easy one:

是我的解决方案,直到我在这里看到一个简单的解决方案:

sort data.txt | uniq -u

回答by Khalil Mejdi

sort data.txt | uniq -c | grep 1\ ?*

sort data.txt | uniq -c | grep 1\ ?*

and it will print the only text that occurs only one time do not forget to put space after the backslash

它将打印仅出现一次的唯一文本 不要忘记在反斜杠后放空格

回答by tink

And one with only one tool in use, awk:

一种只使用一种工具的工具,awk

awk '{a[]++}END{for(i in a){if(a[i] == 1){print i} }}' data.txt