带有管道“|”的多个“awk”语句?

带有管道“|”的多个“awk”语句?
#!/usr/bin/env bash  
#### Extract OS-related info from a Linux box  #### 

#### Display header message ####
# $1 - message

function write_header(){
    local h="$@"
    echo "------------------------------"
    echo "  ${h}"
    echo "------------------------------"
}

#### Get info about Operating System ####

function  os_info(){
    write_header "System Info"
    echo "Operating System : $(uname --kernel-name)" #uname -s 
    echo "Kernel Version   : $(uname --kernel-release)"  #uname -r 
    awk '/^NAME=/||/^VERSION=/' /etc/os-release    
}    

在上面的代码中,我可以从中提取特定字段uname,如下/etc/os-release所示:

System Info
------------------------------
Operating System : Linux
Kernel Version   : 3.16.0-4-amd64
NAME="Debian GNU/Linux"
VERSION="8 (jessie)" 

不过,我无法做的是将awk这样的管道添加awk -F'=' '{print $2}到原始awk语句中,例如: 。我希望最后两行的输出看起来像这样:NAME: Debain GNU/LinuxVERSION: 8 (jessie)

关于如何组合 awk 语句以达到预期结果有什么建议吗?

答案1

awk -F'[="]+' '/^(NAME|VERSION)=/{printf("%-17s: %s\n",$1,$2)}' /etc/os-release

生产

NAME             : BunsenLabs GNU/Linux
VERSION          : 8.6 (Hydrogen)

答案2

正则表达式匹配的替代方案:

awk -F= '$1 == "NAME" || $1 == "VERSION" {print $2}'

答案3

以下似乎对我有用:

awk -F'=' '/^NAME=/||/^VERSION=/ { print $2 }' /etc/os-release

输入:

~$ cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.1 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.1 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial

输出:

~$ awk -F'=' '/^NAME=/||/^VERSION=/ { print $2 }' /etc/os-release
"Ubuntu"
"16.04.1 LTS (Xenial Xerus)"

相关内容