AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题

问题[awk](server)

Martin Hope
Budianto IP
Asked: 2022-11-17 15:09:48 +0800 CST

awk处理多行并返回多个字符串

  • 5

我有这些字符串:

  Certificate Name: domain1.com
    Serial Number: 37aadb8fae7b908e1228c3e71
    Domains: domain1.com www.domain1.com
    Expiry Date: 2022-11-20 02:14:29+00:00 (VALID: 3 days)
    Certificate Path: /etc/letsencrypt/live/domain1.com/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/domain1.com/privkey.pem
  Certificate Name: www.domain2
    Serial Number: 37aadb8fae7b908e1228c3e72
    Domains: www.domain2
    Expiry Date: 2021-10-19 02:15:45+00:00 (INVALID: EXPIRED)
    Certificate Path: /etc/letsencrypt/live/www.domain2/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/www.domain2/privkey.pem
  Certificate Name: domain3.com
    Serial Number: 37aadb8fae7b908e1228c3e73
    Domains: domain3.com www.domain3.com
    Expiry Date: 2022-12-17 02:11:37+00:00 (VALID: 30 days)
    Certificate Path: /etc/letsencrypt/live/domain3.com/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/domain3.com/privkey.pem
  Certificate Name: domain4.com
    Serial Number: 37aadb8fae7b908e1228c3e74
    Domains: domain4.com www.domain4.com
    Expiry Date: 2022-12-10 02:08:54+00:00 (VALID: 23 days)
    Certificate Path: /etc/letsencrypt/live/domain4.com/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/domain4.com/privkey.pem
  Certificate Name: domain5.com
    Serial Number: 37aadb8fae7b908e1228c3e75
    Domains: domain5.com www.domain5.com
    Expiry Date: 2022-11-20 02:14:37+00:00 (VALID: 3 days)
    Certificate Path: /etc/letsencrypt/live/domain5.com/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/domain5.com/privkey.pem
  Certificate Name: domain6.com
    Serial Number: 37aadb8fae7b908e1228c3e76
    Domains: domain6.com www.domain6.com
    Expiry Date: 2022-11-10 02:11:32+00:00 (INVALID: EXPIRED)
    Certificate Path: /etc/letsencrypt/live/domain6.com/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/domain6.com/privkey.pem
  Certificate Name: domain7.com
    Serial Number: 37aadb8fae7b908e1228c3e77
    Domains: domain7.com www.domain7.com
    Expiry Date: 2020-05-31 23:50:48+00:00 (INVALID: EXPIRED)
    Certificate Path: /etc/letsencrypt/live/domain7.com/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/domain7.com/privkey.pem
  Certificate Name: domain8.com
    Serial Number: 37aadb8fae7b908e1228c3e78
    Domains: domain8.com www.domain8.com
    Expiry Date: 2022-11-28 02:14:42+00:00 (VALID: 11 days)
    Certificate Path: /etc/letsencrypt/live/domain8.com/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/domain8.com/privkey.pem  

我习惯使用 sed,但我知道它只会逐行处理。

我已经尝试使用 awk 一段时间了,但由于我不熟悉它,所以我仍在苦苦挣扎。但是,我也愿意接受其他建议。

我只想查找关键字INVALID: EXPIRED,然后在下一行返回域名,最后,它应该有这样的输出:

www.domain2
domain6.com
domain7.com
awk
  • 1 个回答
  • 22 Views
Martin Hope
ihorc
Asked: 2022-02-21 01:20:28 +0800 CST

在 bash 脚本中使用带有列号的 awk

  • 0

我正在使用这样的命令从以下位置获取一些连接nf_conntrack:

awk '($3 == "tcp") && ($6 != "TIME_WAIT") && ($10 == "dport=1234")' /proc/net/nf_conntrack

如何在 bash 脚本中使用此命令并向其添加脚本参数?如果我这样做:

awk '($3 == "tcp") && ($6 != "TIME_WAIT") && ($10 == "dport=$1")' /proc/net/nf_conntrack

它将假设所有$1, $3, $6,... 都是脚本参数并且失败。如何将awk列号与脚本参数分开以正确使用此命令?

bash awk shell-scripting
  • 1 个回答
  • 180 Views
Martin Hope
ITnewbie
Asked: 2021-11-29 20:07:44 +0800 CST

Linux可以做条件判断在Linux的某行插入内容吗

  • 0

Linux可以做条件判断在Linux中的某一行插入内容吗?

例如,我想将用户“test”添加到 /etc/sudoers 让它可以切换到 root:

  1 #
  2 # This file MUST be edited with the 'visudo' command as root.
  3 #
  4 # Please consider adding local content in /etc/sudoers.d/ instead of
  5 # directly modifying this file.
  6 #
  7 # See the man page for details on how to write a sudoers file.
  8 #
  9 Defaults        env_reset
 10 Defaults        mail_badpass
 11 Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
 12 
 13 # Host alias specification
 14 
 15 # User alias specification
 16 
 17 # Cmnd alias specification
 18 
 19 # User privilege specification
 20 root    ALL=(ALL:ALL) ALL
 21 
 22 # Members of the admin group may gain root privileges
 23 %admin ALL=(ALL) ALL
 24 
 25 # Allow members of group sudo to execute any command
 26 %sudo   ALL=(ALL:ALL) ALL
 27 
 28 # See sudoers(5) for more information on "#include" directives:
 29 
 30 #includedir /etc/sudoers.d

我希望我的命令找到第 20 行(如果 root ALL=(ALL:ALL) ALL )然后在第 21 行添加以下内容

test    ALL=(ALL:ALL) ALL

Linux 命令可以做到这一点,还是我只能通过 Perl 或 Python 等编码来做到这一点?

我对 Linux 很陌生,感谢任何帮助!

ubuntu awk sed
  • 1 个回答
  • 33 Views
Martin Hope
louis xie
Asked: 2020-09-08 14:36:05 +0800 CST

使用 bash 在公共键列上左外连接 2 shell 输出

  • 0

有没有一种方法可以根据公共键列(IP 地址)对来自 2 个不同命令的 bash 输出进行“左外连接”?由于各种原因,我想避免将任一命令的输出保存到文件中,并将文件名用作 awk/join 的输入。

这些是我想合并在一起的 2 个输出:

output1=$(oc get pod -lname=elasticsearch -o wide)
output2=$(curl -ks https://elasticsearch.apps.ocp.domain.com/_cat/allocation?v)

这是我的 elasticsearch pod 的磁盘利用率

$ echo "$output1"
shards disk.indices disk.used disk.avail disk.total disk.percent host          ip            node
   347        1.5tb     1.5tb      4.2tb      5.8tb           27 10.1.1.1      10.1.1.1      jxAuPMt
   335        1.7tb     1.7tb        4tb      5.8tb           30 10.2.2.2      10.2.2.2      bwYW1cg
   341        1.3tb     1.9tb    959.7gb      2.9tb           67 10.3.3.3      10.3.3.3      69O4sR-
   343        1.6tb     1.7tb      1.1tb      2.9tb           59 10.4.4.4      10.4.4.4      zCVUS05
   346        1.4tb     1.9tb   1000.6gb      2.9tb           66 10.5.5.5      10.5.5.5      AlvtEtf
   345        1.6tb     1.6tb      1.2tb      2.9tb           57 10.6.6.6      10.6.6.6      pmObkSm
   248          1tb       1tb    374.9gb      1.4tb           74 10.7.7.7      10.7.7.7      OSzB5J1
   332        1.4tb     1.9tb    952.1gb      2.9tb           68 10.8.8.8      10.8.8.8      EShhlrb
   218     1011.8gb       1tb    418.2gb      1.4tb           71 10.9.9.9      10.9.9.9      1rQ7PXD
   329        1.7tb     1.8tb        1tb      2.9tb           63 10.10.10.10   10.10.10.10   73TfUnn
    15                                                                                       UNASSIGNED  

这是我的环境中正在运行的 elasticsearch pod

$ echo "$output2"
NAME                              READY     STATUS    RESTARTS   AGE       IP              NODE
elasticsearch-4-40700             1/1       Running   0          4d        10.1.1.1        server1b.ocp.domain.com
elasticsearch-4-6m2p1             1/1       Running   0          4d        10.2.2.2        server2b.ocp.domain.com
elasticsearch-4-8n8v5             1/1       Running   0          7d        10.3.3.3        server3b.ocp.domain.com
elasticsearch-4-ffnk0             1/1       Running   0          4d        10.4.4.4        server4b.ocp.domain.com
elasticsearch-4-h5xr6             1/1       Running   0          7d        10.5.5.5        server5b.ocp.domain.com
elasticsearch-4-h6c15             1/1       Running   0          7d        10.6.6.6        server6b.ocp.domain.com
elasticsearch-4-j7cv5             1/1       Running   0          7d        10.7.7.7        server7b.ocp.domain.com
elasticsearch-4-nn3pc             1/1       Running   0          5d        10.8.8.8        server8b.ocp.domain.com
elasticsearch-4-sq5hr             1/1       Running   0          7d        10.9.9.9        server9b.ocp.domain.com
elasticsearch-4-zj7tq             1/1       Running   0          7d        10.10.10.10     server10b.ocp.domain.com
elasticsearch-4-az9sp             1/1       Running   0          7d        10.10.10.11     server11b.ocp.domain.com
elasticsearch-4-bi2c7             1/1       Running   0          7d        10.10.10.12     server12b.ocp.domain.com

根据 IP 地址从 output1 和 output2 执行“左外连接”后的预期结果

NAME                              READY     STATUS    RESTARTS   AGE       IP              NODE                      shards disk.indices disk.used disk.avail disk.total disk.percent node
elasticsearch-4-40700             1/1       Running   0          4d        10.1.1.1        server1b.ocp.domain.com      347        1.5tb     1.5tb      4.2tb      5.8tb           27 jxAuPMt 
elasticsearch-4-6m2p1             1/1       Running   0          4d        10.2.2.2        server2b.ocp.domain.com      335        1.7tb     1.7tb        4tb      5.8tb           30 bwYW1cg 
elasticsearch-4-8n8v5             1/1       Running   0          7d        10.3.3.3        server3b.ocp.domain.com      341        1.3tb     1.9tb    959.7gb      2.9tb           67 69O4sR- 
elasticsearch-4-ffnk0             1/1       Running   0          4d        10.4.4.4        server4b.ocp.domain.com      343        1.6tb     1.7tb      1.1tb      2.9tb           59 zCVUS05 
elasticsearch-4-h5xr6             1/1       Running   0          7d        10.5.5.5        server5b.ocp.domain.com      346        1.4tb     1.9tb   1000.6gb      2.9tb           66 AlvtEtf 
elasticsearch-4-h6c15             1/1       Running   0          7d        10.6.6.6        server6b.ocp.domain.com      345        1.6tb     1.6tb      1.2tb      2.9tb           57 pmObkSm 
elasticsearch-4-j7cv5             1/1       Running   0          7d        10.7.7.7        server7b.ocp.domain.com      248          1tb       1tb    374.9gb      1.4tb           74 OSzB5J1 
elasticsearch-4-nn3pc             1/1       Running   0          5d        10.8.8.8        server8b.ocp.domain.com      332        1.4tb     1.9tb    952.1gb      2.9tb           68 EShhlrb 
elasticsearch-4-sq5hr             1/1       Running   0          7d        10.9.9.9        server9b.ocp.domain.com      218     1011.8gb       1tb    418.2gb      1.4tb           71 1rQ7PXD 
elasticsearch-4-zj7tq             1/1       Running   0          7d        10.10.10.10     server10b.ocp.domain.com     329        1.7tb     1.8tb        1tb      2.9tb           63 73TfUnn 
elasticsearch-4-az9sp             1/1       Running   0          7d        10.10.10.11     server11b.ocp.domain.com      
elasticsearch-4-bi2c7             1/1       Running   0          7d        10.10.10.12     server12b.ocp.domain.com

linux bash shell awk coreutils
  • 1 个回答
  • 123 Views
Martin Hope
saffron
Asked: 2020-05-22 09:03:05 +0800 CST

如何使用 awk 分隔变量并使用 ansible 将其附加到文本文件

  • 0

作为 ansible playbook 的额外变量,我得到的是“CHOW_app/timmy_app1/johnn_app3/harper_app4/mona_app5”。这作为单个变量传递给剧本。我必须将其分开并以这种格式保存在文本文件中。

REVOKE CHOW app
REVOKE timmy app1
REVOKE johnn app3
REVOKE harper app4 
REVOKE mona app5

我认为使用 awk 命令可以实现。但我不知道这是否可以在 Ansible 剧本本身中完成。有谁知道我们如何使用 awk 将其分离并将其存储在 Ansible 本身的 txt 文件中。

perl shell awk ansible ansible-playbook
  • 1 个回答
  • 1170 Views
Martin Hope
Kam-ALIEN
Asked: 2020-02-28 03:26:18 +0800 CST

使用 grep 从列中提取字符串

  • 0

我有一个基于列的 .txt 文件,我想用 grep 输出公用名,但有点麻烦。

V   300223164711Z       01  unknown /C=UK/O=LMG/OU=server/CN=server/name=server/[email protected]
V   300223170002Z       02  unknown /C=UK/O=LMF/OU=server/CN=test/name=server/[email protected]
V   300223170935Z       03  unknown /C=UK/O=risk/OU=server/CN=risk/name=server/[email protected]

我当前的 grep 输出执行以下操作:

tail -n +2 index.txt | grep "^V" | cut -d '/' -f 5 | nl -s ') '
     1) CN=test
     2) CN=risk

我只希望名称是输出,“测试”或“风险”。实现这一目标的最简单方法是什么?

shell grep awk
  • 2 个回答
  • 214 Views
Martin Hope
Gagan Pal
Asked: 2018-09-27 05:14:54 +0800 CST

AWK 帮助将数据过滤成 CSV

  • 2

我们正在做一些安全研究,我需要从 Debian repos 中提取所有可用的包名称、版本、desc 等。

我正在尝试将输出解析apt-cache dumpavail为 CSV,并将数据组织成表格形式,如名称、版本、desc。

我不太擅长 AWK,但我想它是完美的工具吗?随意向我推荐我可以为 AWK 制作一个好的正则表达式的方法。

awk
  • 2 个回答
  • 155 Views
Martin Hope
Brian Brownton
Asked: 2017-01-17 11:28:16 +0800 CST

找到另一个字符串后替换一个字符串

  • 0

我正在以编程方式构建盒子,但我需要为 PHP 更新一个 .repo 文件,该文件具有以下内容:

# Repository: http://rpms.remirepo.net/
# Blog:       http://blog.remirepo.net/
# Forum:      http://forum.remirepo.net/

[remi]
name=Remi's RPM repository for Enterprise Linux 6 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/6/remi/$basearch/
mirrorlist=http://rpms.remirepo.net/enterprise/6/remi/mirror
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

[remi-php55]
name=Remi's PHP 5.5 RPM repository for Enterprise Linux 6 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/6/php55/$basearch/
mirrorlist=http://rpms.remirepo.net/enterprise/6/php55/mirror
# NOTICE: common dependencies are in "remi-safe"
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

[remi-php56]
name=Remi's PHP 5.6 RPM repository for Enterprise Linux 6 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/6/php56/$basearch/
mirrorlist=http://rpms.remirepo.net/enterprise/6/php56/mirror
# NOTICE: common dependencies are in "remi-safe"
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

[remi-test]
name=Remi's test RPM repository for Enterprise Linux 6 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/6/test/$basearch/
mirrorlist=http://rpms.remirepo.net/enterprise/6/test/mirror
# WARNING: If you enable this repository, you must also enable "remi"
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

[remi-debuginfo]
name=Remi's RPM repository for Enterprise Linux 6 - $basearch - debuginfo
baseurl=http://rpms.remirepo.net/enterprise/6/debug-remi/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

[remi-php55-debuginfo]
name=Remi's PHP 5.5 RPM repository for Enterprise Linux 6 - $basearch - debuginfo
baseurl=http://rpms.remirepo.net/enterprise/6/debug-php55/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

[remi-php56-debuginfo]
name=Remi's PHP 5.6 RPM repository for Enterprise Linux 6 - $basearch - debuginfo
baseurl=http://rpms.remirepo.net/enterprise/6/debug-php56/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

[remi-test-debuginfo]
name=Remi's test RPM repository for Enterprise Linux 6 - $basearch - debuginfo
baseurl=http://rpms.remirepo.net/enterprise/6/debug-test/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

我只想启用 PHP 5.6,所以我需要找到字符串[remi-php56],然后将它的第一个实例enabled=0更改为enabled=1.

我确定有一个sed或awk为此,但我一直无法弄清楚。

scripting bash awk sed
  • 2 个回答
  • 173 Views
Martin Hope
Bastien974
Asked: 2016-08-27 08:08:31 +0800 CST

使用“:”作为字段分隔符将 grep 结果传递给 awk

  • 2

我想过滤 grep 命令的结果,例如:

myRepo/path/to/my/file.php:123:                             error_log(' - myError');

如果我执行以下操作,它会起作用。

回声“myRepo/path/to/my/file.php:123:error_log('-myError');” | awk -F': ''{打印 $1}'

但是当它是 grep 命令的结果时,它会输出整行,为什么?

grep -rn "我的错误" | awk -F': ''{打印 $1}'

我有 CentOS 6 和 Awk 3.1.7,bash 4.1.2

grep awk
  • 2 个回答
  • 4772 Views
Martin Hope
Vecta
Asked: 2015-07-30 08:32:12 +0800 CST

将 awk 结果限制为 html 请求

  • 0

我正在使用 awk 来分析一些访问日志文件。我目前正在使用以下内容:

awk '($9 ~ /404/)' access_log | awk '{print $9,$7}' | sort | uniq -c | sort > 404.txt

它会返回我的访问日志中的所有 404 以及出现次数。但是,它绝对会返回所有内容——但我只对 html 页面感兴趣。

如何修改它以仅返回以 .html 结尾的请求的值?

awk
  • 1 个回答
  • 81 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve