bash 大于给定数字的数字的 Grep 行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16309942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:38:07  来源:igfitidea点击:

Grep lines for numbers greater than given number

bashshellawk

提问by user2150250

I'm trying to grep for lines in the first field of an output that are greater than a given number. In this instance, that number is 755. Ultimately, what I'm doing is trying to list every file with privileges greater than (and not equal to) 755by using stat -c '%a %n' *and then pipe to some grep'ing (or possibly sed'ing?) to obtain this final list. Any ideas how this could best be accomplished?

我正在尝试 grep 输出的第一个字段中大于给定数字的行。在这种情况下,该数字是755。最终,我正在做的是尝试755通过使用stat -c '%a %n' *然后管道到一些 grep'ing(或可能 sed'ing?)来列出权限大于(且不等于)的每个文件以获得这个最终列表。任何想法如何最好地完成?

回答by Kent

try this:

尝试这个:

stat -c '%a %n' *|awk '>755'

if you just want the filename in your final output, skip the privilege numbers, you could:

如果您只想要最终输出中的文件名,请跳过权限编号,您可以:

stat -c '%a %n' *|awk '>755{print }'

EDIT

编辑

actually you could do the chmodwithin awk. but you should make sure the user execute the awk line has the permission to change those files.

实际上你可以chmod在awk内做。但是您应该确保执行 awk 行的用户有权更改这些文件。

stat -c '%a %n' *|awk '>755{system("chmod 755 ")}'

again, assume the filename has no spaces.

再次假设文件名没有空格。

回答by Carl Norum

I'd use awk(1):

我会用awk(1)

stat -c '%a %n' * | awk ' > 755'

The awkpattern matches lines where the first field is greater then 755. You could add an action if you want to print a subset of a line or something different, too (See @Kent's answer).

awk模式匹配第一个字段大于 755 的行。如果您想打印一行的子集或其他内容,也可以添加一个操作(请参阅@Kent 的回答)。

回答by Lev Levitsky

Neither grepnor sedare good at arithmetics. awkcould help (unfortunately I don't know it). But note that findcan be handy here, too:

既不grep也不sed擅长算术。awk可以提供帮助(不幸的是我不知道)。但请注意,这find在这里也很方便:

   -perm mode
          File's permission bits are exactly  mode  (octal  or  symbolic).
          Since  an  exact match is required, if you want to use this form
          for symbolic modes, you may have to  specify  a  rather  complex
          mode  string.  For example -perm g=w will only match files which
          have mode 0020 (that is, ones for which group  write  permission
          is  the  only  permission set).  It is more likely that you will
          want to use the `/' or `-' forms, for example -perm -g=w,  which
          matches  any file with group write permission.  See the EXAMPLES
          section for some illustrative examples.

   -perm -mode
          All of the permission bits mode are set for the file.   Symbolic
          modes  are accepted in this form, and this is usually the way in
          which would want to use them.  You must specify `u', `g' or  `o'
          if  you use a symbolic mode.   See the EXAMPLES section for some
          illustrative examples.

   -perm /mode
          Any of the permission bits mode are set for the file.   Symbolic
          modes  are  accepted in this form.  You must specify `u', `g' or
          `o' if you use a symbolic mode.  See the  EXAMPLES  section  for
          some  illustrative  examples.  If no permission bits in mode are
          set, this test matches any file (the idea here is to be  consis‐
          tent with the behaviour of -perm -000).

So what could work for you is:

那么对你有用的是:

find . -perm -755 -printf '%m %p\n'

Just remove the -printfpart if you only need filenames.

-printf如果您只需要文件名,只需删除该部分。