使用 awk 对输出进行排序并格式化

发布于 2024-08-13 19:40:56 字数 720 浏览 4 评论 0原文

我正在尝试将 ls -la 的输出格式化为仅包含 12 月修改的文件并很好地输出它们,这就是它们当前的样子:

ls -la | awk {'print $6,$7,$8,$9,$10'} | grep "Dec" | sort -r | head -5
Dec 4 20:15 folder/
Dec 4 19:51 ./
Dec 4 17:42 Folder\ John/
Dec 4 16:19 Homework\ MAT\ 08/
Dec 4 16:05 Folder\ Smith/

等等。

我如何设置类似正则表达式不包含“./”和“../”之类的内容,

另外,对于其中包含空格的文件夹,如何省略斜杠“\”。我想在最后去掉斜线。这可以通过 shell 命令实现吗?或者我必须使用 Perl 对测试进行修改吗?我确实希望日期和时间保持原样。任何帮助将不胜感激!

该盒子装有 Linux,这是通过 SSH 完成的。

编辑:

这是我到目前为止所拥有的(感谢 Mark 和 gbacon),

ls -laF | grep -vE ' ..?/?$' | awk '{ for (i=6; i<=NF; i++) printf("%s ", $i); printf("\n"); } ' | grep "Dec" | sort -r | head -5

我只是在用空格“”替换“\”时遇到了麻烦。除此之外,感谢到目前为止的所有帮助!

I'm trying to format the output of ls -la to only contain files modified in December and output them nicely, this is what they currently look like:

ls -la | awk {'print $6,$7,$8,$9,$10'} | grep "Dec" | sort -r | head -5
Dec 4 20:15 folder/
Dec 4 19:51 ./
Dec 4 17:42 Folder\ John/
Dec 4 16:19 Homework\ MAT\ 08/
Dec 4 16:05 Folder\ Smith/

etc..

How can I set up something like a regular expression to not include things like "./" and "../",

Also how can I omit the slash "\" for folders that have spaces in them. Id like to drop the slash at the end. Is this possible through a shell command? Or would I have to use Perl to make modifications to the test? I do want the date and time to remain as is. Any help would be greatly appreciated!

The box has linux and this is being done via SSH.

Edit:

Heres what I have so far (thanks to Mark and gbacon for this)

ls -laF | grep -vE ' ..?/?

Im just having trouble with replacing "\ " with just a space " ". Other than that Thanks for all the help upto this point!

| awk '{ for (i=6; i<=NF; i++) printf("%s ", $i); printf("\n"); } ' | grep "Dec" | sort -r | head -5

Im just having trouble with replacing "\ " with just a space " ". Other than that Thanks for all the help upto this point!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

失而复得 2024-08-20 19:40:56

您可以使用 find 为您完成大部分工作:

find -mindepth 1 -maxdepth 1 -printf "%Tb %Td %TH:%TM %f\n" | grep "^Dec" | sort -r

默认情况下不包含父目录 (..)。 -mindepth 1 删除当前目录 (.)。您可以删除 -maxdepth 1 以使其递归,但您应该将 %f 更改为 %p 以包含带有文件名的路径。

这些是 -printf 中的字段:

  • %Tb - 短月份名称
  • %Td - 该月的日期
  • %TM:%TM - 小时和分钟
  • %f - 文件名

grep

You can use find to do most of the work for you:

find -mindepth 1 -maxdepth 1 -printf "%Tb %Td %TH:%TM %f\n" | grep "^Dec" | sort -r

The parent directory (..) is not included by default. The -mindepth 1 gets rid of the current directory (.). You can remove the -maxdepth 1 to make it recursive, but you should change the %f to %p to include the path with the filename.

These are the fields in the -printf:

  • %Tb - short month name
  • %Td - day of the month
  • %TM:%TM - hours and minutes
  • %f - filename

In the grep I've added a match for the beginning of the line so it won't match a file named "Decimal" that was modified in November, for example.

梦罢 2024-08-20 19:40:56

检查并确保您的“ls”命令没有别名为其他命令。通常,“raw”ls 不会为您提供目录的 / ,也不应该转义空格。

显然,有一些东西正在为您转义空格,以便您的 awk 打印这些文件,因为 awk 倾向于用空格分隔字段,这就是 \ 字符的用途。

空格是文件名,专门设计用于阻止编写简单的脚本和管道混搭,就像您在此处尝试执行的那样。

Check and make sure your 'ls' command isn't aliased to something else. Typically, "raw" ls doesn't give you the / for directories, nor should it be escaping the spaces.

Clearly something is escaping the spaces for you for your awk to be printing those files, since awk tends to break field up by whitespace, that's what the \ characters are for.

Spaces is files names are designed specifically to frustrate writing easy script and pipe mashups like you're are trying to do here.

三生池水覆流年 2024-08-20 19:40:56

您可以过滤 ls 的输出:

ls -la | grep -vE ' ..?/?

如果您愿意使用 Perl:

ls -la | perl -lane 's/\\ / /g;
                     print "@F[5..9]"
                       if $F[8] !~ m!^..?/?$! &&
                          $F[5] eq "Dec"'
| awk {'print $6,$7,$8,$9,$10'} | grep "Dec" | sort -r | head -5

如果您愿意使用 Perl:

You could filter the output of ls:

ls -la | grep -vE ' ..?/?

If you're content to use Perl:

ls -la | perl -lane 's/\\ / /g;
                     print "@F[5..9]"
                       if $F[8] !~ m!^..?/?$! &&
                          $F[5] eq "Dec"'
| awk {'print $6,$7,$8,$9,$10'} | grep "Dec" | sort -r | head -5

If you're content to use Perl:

一个人的旅程 2024-08-20 19:40:56

这是您的答案之一:

如何设置正则表达式之类的内容以不包含“./”和“../”等内容,

使用 ls -lA 而不是 ls -la

您可以打印从第 6 列到行尾的所有内容,而不是打印固定数量的列:

ls -lA | awk '{ for (i=6; i<=NF; i++) printf("%s ", $i); printf("\n"); } '

我没有得到反斜杠的空格,所以我不知道为什么会得到这个。要修复它,您可以添加以下内容:

| sed 's/\\//g'

Here's one of your answers:

How can I set up something like a regular expression to not include things like "./" and "../",

Use ls -lA instead of ls -la.

Instead of printing out a fixed number of columns, you can print out everything from column 6 t the end of the line:

ls -lA | awk '{ for (i=6; i<=NF; i++) printf("%s ", $i); printf("\n"); } '

I don't get the spaces backslashed, so I don't know why you are getting that. To fix it you could add this:

| sed 's/\\//g'
淡笑忘祈一世凡恋 2024-08-20 19:40:56

所有的 grep 和 sed 是怎么回事???

ls -laF | awk '!/\.\.\/$/ && !/\.\/$/ &&/Dec/ { for (i=6; i<=NF; i++) printf("%s ", $i); printf("\n"); }'

what's with all the greps and seds???

ls -laF | awk '!/\.\.\/$/ && !/\.\/$/ &&/Dec/ { for (i=6; i<=NF; i++) printf("%s ", $i); printf("\n"); }'
要走干脆点 2024-08-20 19:40:56

那么你可以通过添加 grep -v "\." 来删除 ... | grep -v "\.\."

不确定其余的

well you can drop . and .. by adding grep -v "\." | grep -v "\.\."

not sure about the rest

攀登最高峰 2024-08-20 19:40:56

看到带有 awk 和 grep/sed 的管道真的让我很恼火。 awk 是一个非常强大的行处理工具。

ls -laF | awk '
    / \.\.?\/$/ {next}
    / Dec / {for (i=1; i<=5; i++) $i = ""; print} 
' | sort -r | head -5

It really irks me to see pipelines with awk and grep/sed. Awk is a very powerful line-processing tool.

ls -laF | awk '
    / \.\.?\/$/ {next}
    / Dec / {for (i=1; i<=5; i++) $i = ""; print} 
' | sort -r | head -5
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文