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

前端 未结 8 1625
暗喜
暗喜 2021-01-11 17:37

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

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

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-11 18:22

    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.

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

    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 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).

    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.

    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.

提交回复
热议问题