Grep match only before “:”

前端 未结 3 1578

Hello How can I grep only match before : mark?

If I run grep test1 file, it shows all three lines.

test1:x:29688:test1,test2
te         


        
相关标签:
3条回答
  • As your data is in row, columns delimited by a character, you may consider awk:

    awk -F: '$1 == "test1"' file
    
    0 讨论(0)
  • 2021-01-26 17:18

    If the desired lines always start with test1 then you can do:

    grep '^test1' file
    

    If it's always followed by : but not the other (potential) matches then you can include it as part of the pattern:

    grep 'test1:' file
    
    0 讨论(0)
  • 2021-01-26 17:30

    I think that you just need to add “:” after “test1”, see an example:

    grep “test1:” file
    
    0 讨论(0)
提交回复
热议问题