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

前端 未结 8 1618
暗喜
暗喜 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:18

    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

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-11 18:23

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

    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'

    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

    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.

    0 讨论(0)
  • 2021-01-11 18:23

    The following will get you what you need.

    grep 'password' data.txt 
    
    0 讨论(0)
  • 2021-01-11 18:24
    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
    
    0 讨论(0)
  • 2021-01-11 18:30

    And one with only one tool in use, awk:

    awk '{a[$1]++}END{for(i in a){if(a[i] == 1){print i} }}' data.txt
    
    0 讨论(0)
提交回复
热议问题