问题
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