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

时间:2022-08-31 15:57:07

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查找特定行?

7 个解决方案

#1


17  

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.

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

#2


5  

To provide some context (I need more rep to comment) this is a question that features in an online "wargame" called Bandit that 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 here however it looks like this:

对于那些想要完全看到data.txt的人,我在这里粘贴它,但它看起来像这样:

NN4e37KW2tkIb3dC9ZHyOPdq1FqZwq9hjpEYciZvDIs6MLPhYoOGWQHNIoQZzE5q3rpovhi1CyT7RUTunW30goGek5Q5Fu66JOaWd4uAPii4Jc19AP2McmBNRzBYDAkOJOaWd4uAPii4Jc19AP2McmBNRzBYDAkO9WV67QT4uZZK7JHwmOH0jnhurJMwoGZUa2GjmWtTe3tTM0ARl7TQwraPGXgfkH4f7yJ8imXc7NNiovDuAl1ZC6xb0O0mMBx1UsvVyFSfZZWbi6wgC7dAFyFuR6jQQUhRFcOJhZkHlnwqcD8QbvjRyn886rCrnWZ7E3ugYDa6Wh2y8C8xQev7vOS8O3OgG1HwE3ugYDa6Wh2y8C8xQev7vOS8O3OgG1HwME7nnzbId4W3dajsl6Xtviyl5uhmMenvJ5lN3Qe4s7ktiwvcCj9ZHWrAJcUWEhUqaouHvjzagN8QT2BCMB6e9rlN4ffqZ0QqZRF5dlSuwuVV9TLhHKvPvRDrQ2L5ODfD9ZjR3NTHue4YR6n4DgG5e0qMQcJjTaiMQT8Bw9ofH4x3MeRvYAVbYvV1e1zq3Ximi6A6TL6nqvjCAPvOdXZWjlYgyvqxmB7ktx7tQ6kgeJnC446CHbiJY7fyRwrwuhrs

One way to do it is to use:

一种方法是使用:

sort data.txt | uniq -u

The sort command is like cat in that it displays the contents of the file however it sorts the file lexicographically by 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 uniq command reports or omits repeated lines and by passing it the -u argument 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进行排序,找到唯一的行并将其打印回终端。

#3


1  

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

添加更多信息给您发布。 data.txt的外观如何?像这样:

1111111111111111pass111111111111

Or like this

或者像这样

afawfdgdpasswordsomethingelse...

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'密码'

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.

当然用其他东西替换传球。例如,来自线的一些切片。

#4


1  

And one with only one tool in use, awk:

并且只使用一个工具,awk:

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

#5


0  

The following will get you what you need.

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

grep 'password' data.txt 

#6


0  

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 timedo not forget to put space after the backslash

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

#7


0  

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

#1


17  

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.

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

#2


5  

To provide some context (I need more rep to comment) this is a question that features in an online "wargame" called Bandit that 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 here however it looks like this:

对于那些想要完全看到data.txt的人,我在这里粘贴它,但它看起来像这样:

NN4e37KW2tkIb3dC9ZHyOPdq1FqZwq9hjpEYciZvDIs6MLPhYoOGWQHNIoQZzE5q3rpovhi1CyT7RUTunW30goGek5Q5Fu66JOaWd4uAPii4Jc19AP2McmBNRzBYDAkOJOaWd4uAPii4Jc19AP2McmBNRzBYDAkO9WV67QT4uZZK7JHwmOH0jnhurJMwoGZUa2GjmWtTe3tTM0ARl7TQwraPGXgfkH4f7yJ8imXc7NNiovDuAl1ZC6xb0O0mMBx1UsvVyFSfZZWbi6wgC7dAFyFuR6jQQUhRFcOJhZkHlnwqcD8QbvjRyn886rCrnWZ7E3ugYDa6Wh2y8C8xQev7vOS8O3OgG1HwE3ugYDa6Wh2y8C8xQev7vOS8O3OgG1HwME7nnzbId4W3dajsl6Xtviyl5uhmMenvJ5lN3Qe4s7ktiwvcCj9ZHWrAJcUWEhUqaouHvjzagN8QT2BCMB6e9rlN4ffqZ0QqZRF5dlSuwuVV9TLhHKvPvRDrQ2L5ODfD9ZjR3NTHue4YR6n4DgG5e0qMQcJjTaiMQT8Bw9ofH4x3MeRvYAVbYvV1e1zq3Ximi6A6TL6nqvjCAPvOdXZWjlYgyvqxmB7ktx7tQ6kgeJnC446CHbiJY7fyRwrwuhrs

One way to do it is to use:

一种方法是使用:

sort data.txt | uniq -u

The sort command is like cat in that it displays the contents of the file however it sorts the file lexicographically by 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 uniq command reports or omits repeated lines and by passing it the -u argument 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进行排序,找到唯一的行并将其打印回终端。

#3


1  

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

添加更多信息给您发布。 data.txt的外观如何?像这样:

1111111111111111pass111111111111

Or like this

或者像这样

afawfdgdpasswordsomethingelse...

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'密码'

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.

当然用其他东西替换传球。例如,来自线的一些切片。

#4


1  

And one with only one tool in use, awk:

并且只使用一个工具,awk:

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

#5


0  

The following will get you what you need.

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

grep 'password' data.txt 

#6


0  

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 timedo not forget to put space after the backslash

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

#7


0  

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