【问题标题】:Filtering a list based on sed and awk command, multiple delimiter基于 sed 和 awk 命令过滤列表,多个分隔符
【发布时间】:2021-08-29 22:34:33
【问题描述】:

我在这个问题上需要帮助。 我的文件夹中有一些文件,我想列出它,但我只想列出主要的课程名称。

-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - Bauch-Beine-Po 01.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - Bauch-Beine-Po 02.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - Bauch-Beine-Po 03.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - Bauch-Beine-Po 04.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Bauch-Beine-Po 05.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Bauch-Beine-Po 06.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Bauch-Beine-Po 07.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Bauch-Beine-Po 08.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Bauch-Beine-Po 09.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Bauch-Beine-Po 10.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Bauch-Beine-Po 11.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Entspannung.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Muskeln wie Stahl 01.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Muskeln wie Stahl 02.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Muskeln wie Stahl 03.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Muskeln wie Stahl 04.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Muskeln wie Stahl 05.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Muskeln wie Stahl 06.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Muskeln wie Stahl 07.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - RückenFit 01.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - RückenFit 02.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - RückenFit 03.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - RückenFit 04.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - RückenFit 05.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - Stretching 01.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - Stretching 02.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - Stretching 03.mp4'

这里

输出应该是这样的:

鲍赫-贝内-波

转折

穆斯克尔恩·斯塔尔

RückenFit

拉伸

我试过这段代码,但它不是我想要的:

ls -l | awk -F'[ .\$]' '{print $11 }' | uniq 

感谢您提前提供帮助

【问题讨论】:

  • 为什么是图片。获取里面的文字并粘贴有那么难吗?
  • 解析ls 输出很笨重。
  • 当你无法准确描述预期的输出时,你就无法对其进行编程。
  • 你是对的@Roadowl,对不起,我是新来的,我一直承受着压力。很清楚我现在在寻找什么!谢谢
  • 嗨@SaeidHerawi,欢迎来到该网站!如果您粘贴在图像中显示的文本,我相信您的问题将得到解决 - 然后我们可以将文本复制到我们的系统并处理命令以从中获取您需要的名称。如果我们没有得到文本,我们就很难做到这一点(很少有人会花时间在图像中手动输入示例来处理它,而且很少有人会如此自信地提供他们只认为 可以工作,但尚未在他们的系统上进行测试)。

标签: awk sed delimiter cut


【解决方案1】:

你也可以使用 awk。

使用 - 作为字段分隔符,并从最后一个字段中删除可选的空格和后跟扩展名的数字。

例如

ls -A1 | awk -F ' - ' '{sub(/( [0-9]+)?\.[^.]+$/, "", $NF);print $NF}' | sort | uniq

在示例代码中:

  • $NF是最后一个字段,会先被sub修改,然后打印出来
  • 模式 ( [0-9]+)?\.[^.]+$ 匹配可选空格和 1+ 个数字,后跟一个点和除点之外的任何字符,直到字符串末尾

【讨论】:

    【解决方案2】:

    这正是我想要的

    ls -lrtA |cut -f11-13 -d ' '| sed  's/[0-9]*\.[^.]*$//'| sort | uniq
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-03
      • 1970-01-01
      • 2012-12-28
      • 2016-07-31
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 2018-08-15
      相关资源
      最近更新 更多