Finding lines which are greater than 120 characters length using sed

六眼飞鱼酱① 提交于 2020-12-29 10:03:36

问题


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 this ? Is there any other way to get a list other than using sed ?? Thanks..


回答1:


Another way to do this using awk:

cat file | awk 'length($0) > 120'



回答2:


You can use grep and its repetition quantifier:

grep '.\{120\}' script.sh



回答3:


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.



来源:https://stackoverflow.com/questions/12815519/finding-lines-which-are-greater-than-120-characters-length-using-sed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!