Finding lines which are greater than 120 characters length using sed

后端 未结 3 999
旧时难觅i
旧时难觅i 2021-02-13 22:42

I want to get a list of lines in a batch file which are greater than 120 characters length. For this I thought of using sed. I tried but I was not successful. How can i achieve

相关标签:
3条回答
  • 2021-02-13 23:05

    Another way to do this using awk:

    cat file | awk 'length($0) > 120'
    
    0 讨论(0)
  • 2021-02-13 23:12

    You can use grep and its repetition quantifier:

    grep '.\{120\}' script.sh
    
    0 讨论(0)
  • 2021-02-13 23:20

    Using sed, you have some alternatives:

    sed -e '/.\{120\}/!d'
    sed -e '/^.\{,119\}$/d'
    sed -ne '/.\{120\}/p'
    

    The first option matches lines that don't have (at least) 120 characters (the ! after the expression is to execute the command on lines that don't match the pattern before it), and deletes them (ie. doesn't print them).

    The second option matches lines that from start (^) to end ($) have a total of characters from zero to 119. These lines are also deleted.

    The third option is to use the -n flag, which tells sed to not print lines by default, and only print something if we tell it to. In this case, we match lines that have (at least) 120 characters, and use p to print them.

    0 讨论(0)
提交回复
热议问题