根据单词数组排除标准输出 [英] exclude stdout based on an array of words

查看:55
本文介绍了根据单词数组排除标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以排除某些基于单词数组输出的命令?**

Is it possible to exclude some of a commands output its output based on an array of words?**

在Ubuntu/Debian上,有两种列出所有可用pkg的方法:

On Ubuntu/Debian there are two ways to list all available pkgs:

  1. apt列表(显示所有可用的pkgs,也显示已安装的pkgs)
  2. apt-cache搜索.(显示所有可用的pkgs,也显示已安装的pkgs)
  1. apt list (show all available pkgs, installed pkgs too)
  2. apt-cache search . (show all available pkgs, installed pkgs too)

区别是,第一个命令,您可以使用 grep -v 排除所有已安装的pkgs,问题不像第一个,第二个命令则不能排除这些已安装的pkg.不存在.第一个命令的问题是它没有显示pkg描述,因此我想使用 apt-cache search.,但要排除所有已安装的pkgs.

Difference is, the first command, you can exclude all installed pkgs using grep -v, problem is unlike the first, the second command you cannot exclude these as the word "installed" isnt present. Problem with the first command is it doesnt show the pkg description, so I want to use apt-cache search . but excluding all installed pkgs.

# List all of my installed pkgs,
# Get just the pkg's name,
# Swap newlines for spaces,
# Save this list as an array for exclusion.

INSTALLED=("$(apt list --installed 2> /dev/null | awk -F/ '{print $1}' | tr '\r\n' ' ')")

然后我尝试:

apt-cache search . | grep -v "${INSTALLED[@]}"

不幸的是,这不起作用,因为我仍然看到已安装的pkg,所以我猜想它不包括阵列中的第一个pkg,而不是其余的.

Unfortunately this doesnt work as I still see my installed pkgs too so I'm guessing its excluding the first pkg in the array and not the rest.

再次感谢您!

推荐答案

请尝试以下操作:

#!/bin/bash

declare -A installed                            # an associative array to memorize the "installed" command names

while IFS=/ read -r cmd others; do              # split the line on "/" and assign the variable "cmd" to the 1st field
    (( installed[$cmd]++ ))                     # increment the array value associated with the "$cmd"
done < <(apt list --installed 2> /dev/null)     # excecute the `apt` command and feed the output to the `while` loop

while IFS= read -r line; do                     # read the whole line "as is" because it includes a package description
    read -r cmd others <<< "$line"              # split the line on the whitespace and assign the variable "cmd" to the 1st field
    [[ -z ${installed[$cmd]} ]] && echo "$line" # if the array value is not defined, the cmd is not installed, then print the line
done < <(apt-cache search .)                    # excecute the `apt-cache` command to feed the output to the `while` loop

  • 已安装关联数组 用于检查命令是否为已安装.
  • 第一个 while 循环扫描命令的已安装列表,然后将命令名称存储在已关联的已安装数组中.
  • 第二个 while 循环扫描可用的命令列表,以及是否在关联数组中找不到命令,然后打印出来.
    • The associative array installed is used to check if the command is installed.
    • The 1st while loop scans over the installed list of the command and store the command names in the associative array installed.
    • The 2nd while loop scans over the available command list and if the command is not found in the associative array, then print it.
    • 顺便说一句,您的试用代码以#!/bin/sh 开头,该代码以 sh 运行,而不是 bash .请确保它看起来像#!/bin/bash .

      BTW your trial code starts with #!/bin/sh which is run with sh, not bash. Please make sure it looks like #!/bin/bash.

      这篇关于根据单词数组排除标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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