gpt4 book ai didi

linux - 如何在 shell 脚本中将多个 shell 变量传递给 awk?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:15:15 25 4
gpt4 key购买 nike

我已经编写了一个 shell 脚本来获取任何列。脚本是:

#!/bin/sh
awk '{print $c}' c=${1:-1}

这样我就可以称它为

ls -l | column 2

但是我如何为多个列实现它呢?说吧,我想要这样的东西:

ls -l | column 2 3

最佳答案

在这种情况下,我根本不会使用 awk:

columns() { tr -s '[:blank:]' ' ' | cut -d ' ' -f "$@"; }

这使用 tr 将所有空白序列压缩到一个空格,然后 cut 提取您感兴趣的字段。

ls -l | columns 1,5,9-

注意,你shouldn't parse the output of ls .

关于linux - 如何在 shell 脚本中将多个 shell 变量传递给 awk?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17882590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com