如何在命令行中使用带有cut的正则表达式? [英] How to use regex with cut at the command line?

查看:286
本文介绍了如何在命令行中使用带有cut的正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 ls -alth 得到一些输出:

drwxr-xr-x    5 root    admin   170B Aug  3  2016 ..
drwxr-xr-x    5 root    admin    70B Aug  3  2016 ..
drwxr-xr-x    5 root    admin     3B Aug  3  2016 ..
drwxr-xr-x    5 root    admin     9M Aug  3  2016 ..

现在,我想解析 170B 部分,这显然是人类可读格式的大小。我想使用 cut sed 进行此操作,因为我不想使用更复杂的工具/ p难以使用。

Now, I want to parse out the 170B part, which is obviously the size in human readable format. I wanted to do this using cut or sed, because I don't want to use tools that are any more complicated/difficult to use than necessary.

理想情况下,我希望它足够强大以处理 B M 或随大小附带的 K 后缀,并相应地乘以 1 1000000 1000

Ideally I want it to be robust enough to handle the B, M or K suffix that comes with the size, and multiply accordingly by 1, 1000000 and 1000 accordingly. I haven't found a good way to do that, though.

我尝试了几件事却并不真正了解最佳方法:

I've tried a few things without really knowing the best approach:

ls -alth | cut -f 5 -d \s+

我希望这样做会起作用,因为我能够只需在一个或多个空格上将其定界。

I was hoping that would work because I'd be able to just delimit it on one or more spaces.

但这是行不通的。如何为 cut 提供正则表达式分隔符?还是有一种更简单的方法从 ls -alth 中仅提取文件的大小?

But that doesn't work. How do I supply cut with a regex delimiter? or is there an easier way to extract only the size of the file from ls -alth?

我是使用CentOS6.4

I'm using CentOS6.4

推荐答案

此答案解决了所要求的问题,但请考虑< a href = https://stackoverflow.com/a/43312948/45375> George Vasiliou有用的查找解决方案作为潜在的优越选择。

This answer tackles the question as asked, but consider George Vasiliou's helpful find solution as a potentially superior alternative.


  • cut 仅支持单个,文字字符作为分隔符( -d ),因此它不是使用的正确工具。

  • cut only supports a single, literal character as the delimiter (-d), so it isn't the right tool to use.

对于提取以每行空白量分隔的令牌(字段), awk 是最好的工具,因此乔治·瓦西里乌(George Vasiliou)是最简单的一个:

ls -alth | awk'{print $ 5}'

提取第5个空格分隔的字段( $ 5 ),即大小。

For extracting tokens (fields) that are separated with a variable amount of whitespace per line, awk is the best tool, so the solution proposed by George Vasiliou is the simplest one:
ls -alth | awk '{print $5}'
extracts the 5th whitespace-separated field ($5), which is the size.

而不是先使用 -h ,然后重新转换人类可读的后缀(例如 B M G )回到单纯的 byte 计数(顺便说一句,乘数必须是 1024 的倍数,而不是 1000 的倍数),只需省略 -s 命令中的 -h ,默认情况下输出原始字节数:

ls -alt | awk’{print $ 5}’ c $

Rather than use -h first and then reconvert the human-readable suffixes (such as B, M, and G) back to the mere byte counts (incidentally, the multipliers must be multiples of 1024, not 1000), simply omit -h from the ls command, which outputs the raw byte counts by default:
ls -alt | awk '{print $5}'

这篇关于如何在命令行中使用带有cut的正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆