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
    • 最新
    • 标签
主页 / user-342207

peti27's questions

Martin Hope
peti27
Asked: 2025-01-16 00:20:57 +0800 CST

如何选择由两个不同的字符串模式分隔的字符串

  • 4

我有一个类似于下面的文件(filename whatever.com)的输出......

[...]~ # tmsh list sys file ssl-cert whatever.com_2024
sys file ssl-cert whatever.com_2024 {
    certificate-key-size 2048
    checksum SHA1:2520:ab40df7776dbbccb62345560511f2205d
    create-time 2024-08-12:19:34:07
    created-by x.y
    expiration-date 1754956799
    expiration-string "Aug 11 23:59:59 2025 GMT"
    fingerprint SHA256/D8:57:8E:8E:4A:1A:C3:3C:1B:6F:32:59:A7:36:66:92:6C
    issuer "CN=DigiCert Global G2 TLS RSA SHA256 2020 CA1,O=DigiCert Inc,C=US"
    key-type rsa-public
    last-update-time 2024-08-12:19:34:07
    mode 33188
    revision 1
    serial-number 0a:1d:ca:c7:09:7b:49:59:b2
    size 2520
    source-path /var/run/key_mgmt/RvGubB/ssl.crt/whatever.com_2024
    subject "CN=whatever.com,O=XYZ,L=Toronto,ST=Ontario,C=CA"
    subject-alternative-name "DNS:whatever.com"
    updated-by x.y
    version 3

我正在尝试根据第 17 行(以主题开头)上面的信息生成命令输出

cat whatever.com | awk 'BEGIN  {F=" "; FS = "\n"; RS = ""; OFS = "\n"} { print "openssl req -new -nodes -sha256 -newkey rsa:2048 -out "substr($1,RSTART+19,length($1)-25)"_2025.csr -keyout "substr($1,RSTART+19,length($1)-25)"_2025.key -subj "/"substr($17,RSTART+14,length($17)-14)"\""}'

输出看起来不错

openssl req -new -nodes -sha256 -newkey rsa:2048 -out whatever.com_2025.csr -keyout whatever.com_2025.key -subj "/CN=whatever.com,O=XYZ,L=Toronto,ST=Ontario,C=CA"

但是它使用第 1 行的子字符串,该子字符串可能与第 17 行不同,所以我想获取文件名,而不是使用 substr($1,RSTART+19,length($1)-24) 根据“CN=”和“,O=”之间的字符串形式 $17 生成。

如果第 18 行包含字符串“DNS”,那么是否可以将输出修改为如下所示...

openssl req -new -nodes -sha256 -newkey rsa:2048 -out whatever.com_2025.csr -keyout whatever.com_2025.key -subj "/CN=whatever.com,O=XYZ,L=Toronto,ST=Ontario,C=CA" -addext "subjectAltName = DNS:whatever.com"

感谢大家的快速回答。文件名必须从文件中动态生成。包含的示例只是较大输出的一小部分。BEGIN 配置用于创建这些部分,此时可以按行单独处理。

处理 F5 负载平衡器。它们基于 Linux,但并不完全像普通服务器那样构建,这使得一些脚本编写具有挑战性。

我找到了使用 split() 的解决方案,但现在我遇到了另一个问题。当我单独运行该命令时,它似乎有效

 ~ # cat whatever.com | awk 'BEGIN  {F=" "; FS = "\n"; RS = ""; OFS = "\n"} {split($17,a,/,/); print substr(a[1],RSTART+17)}'
whatever.com

然而,当添加到原始脚本中时,它会在前面添加数字“5”。

 ~ # cat whatever.com |  awk 'BEGIN  {F=" "; FS = "\n"; RS = ""; OFS = "\n"} { print "openssl req -new -nodes -sha256 -newkey rsa:2048 -out "split($17,a,/,/); print substr(a[1],RSTART+17)"_2025.csr -keyout "split($17,a,/,/); print substr(a[1],RSTART+17)"_2025.key -subj \"/"substr($17,RSTART+14,length($17)-14)"\""}' 

openssl req -new -nodes -sha256 -newkey rsa:2048 -out 5
whatever.com_2025.csr -keyout 5
whatever.com_2025.key -subj "/CN=whatever.com,O=XYZ,L=Toronto,ST=Ontario,C=CA"
awk
  • 4 个回答
  • 84 Views
Martin Hope
peti27
Asked: 2023-09-07 22:09:54 +0800 CST

如何使用 awk 重新创建 grep -f

  • 8

我有一个大文件,其中有很多不必要的信息。我只对编辑和下一个之间的部分感兴趣,并将它们作为一个条目处理。我设法像这样过滤掉它......

'

awk  'BEGIN {FS = "\n"; RS = ""; OFS = "\n" ;} {if (/intf/ && /addr/) { print $0"\n"}}' > outputfile

输出文件的示例如下所示。

edit 114
set uuid 6cb43
set action accept
set srcintf "Port-ch40.1657"
set dstintf "any"
set srcaddr "1.1.1.1"
set dstaddr "all"
set schedule "always"
set service "ALL_ICMP" "icmp-echo-reply" "icmp-source-quench" "icmp-time-exceeded" "icmp-unreachable"
set logtraffic all
next

edit 330
set uuid 6d3d
set action accept
set srcintf "Po40.28"
set dstintf "any"
set srcaddr "all"
set dstaddr "2.2.2.2"
set schedule "always"
set service "ALL_ICMP" "icmp-echo-reply" "icmp-source-quench" "icmp-time-exceeded" "icmp-unreachable"
set logtraffic all
next

grep 有一个选项,其中值可以来自文件(grep -f filterfile textfile)让我们假设 filterfile 包含值...

1.1.1.1
3.3.3.3

实际上会更多,因此手动输入可能不起作用。

awk  'BEGIN {FS = "\n"; RS = ""; OFS = "\n" ;} {if (/intf/ && /addr/ &&(/1.1.1.1/||/3.3.3.3/)) { print $0"\n"}}' > outputfile

是否可以修改 awk 命令来处理来自文件的值

awk  'BEGIN {FS = "\n"; RS = ""; OFS = "\n" ;} {if (/intf/ && /addr/ &&(values_from_filterfile)) { print $0"\n"}}' > outputfile
awk
  • 3 个回答
  • 90 Views
Martin Hope
peti27
Asked: 2019-11-14 11:35:56 +0800 CST

结合 if 和 for 的 awk 命令

  • 0

我有一个 20 列的 csv 文件。文件的第一部分是地理位置信息。我想要做的是找到某个记录的地理位置数据。首先如果标识用户“ROSA”。在该行中搜索前 10 列,如果其中任何列包含“GeoLocation”,则仅打印下一条记录而不是整行。

awk  -F"," '($0~/ROSA/) {for(i=1;i<=10;i++) {($i~/GeoLocation/) print $(i+1)}}' filename.csv

我想出了上面的行,但我在打印中的“p”处被标记为语法错误。

你们能发现我错过了什么吗?

awk
  • 2 个回答
  • 65 Views
Martin Hope
peti27
Asked: 2019-07-31 07:10:40 +0800 CST

使用 AWK 根据另一个文件中的值更新一个文件

  • 0

我有两个文件 file1.csv(20 列 410k 行)和 data.csv(4 列 1800 行)。我想要做的是,如果 data.csv 第一列与 file1.csv 第二列匹配,用 data.csv 第三列中的值覆盖 file1.csv 中的第一列。如果没有匹配保留现有值...

此命令将列出匹配的行,但无法正确获取替换部分。

awk -F"," 'BEGIN{OFS=","} {if (NR==FNR) {a[$1]=$3; next} if ($2 in a) print}' data.csv file1.csv
 > file3.csv

谢谢!

awk merge
  • 1 个回答
  • 1085 Views
Martin Hope
peti27
Asked: 2019-03-18 08:32:41 +0800 CST

将 awk 命令转为 awk 脚本(多 F)

  • 1

我有一个工作命令

awk  -F '[)#/(:]' 'BEGIN  { fw="";dev=""} {if ($0~/sh failover/) fw=$1 ; if (($0~/This host/)||($0~/Other host/)) dev=$2; if ($0~/\)\:/) {print $2,$1,fw, dev} }' OFS="|" test_data

我想把它变成一个脚本。这样做时...

#!/bin/sh
awk '
BEGIN  {F='[)#/(:]'; FS = "\n"; RS = ""; OFS = "|";fw="";dev=""} 
{
    if ($0~/sh failover/) fw=$1 ; 
    if (($0~/This host/)||($0~/Other host/)) dev=$2; 
    if ($0~/\)\:/) {
        print $2,$1,fw, dev
        } 
}' test_data 

... F='[)#/(:]' 导致错误。

[...srv01]$ ./test
./test: line 3: syntax error near unexpected token `)'
./test: line 3: `BEGIN  {F='[)#/(:]'; FS = "\n"; RS = ""; OFS = "|";fw="";dev=""} '
[...srv01]$ 

当更改为双引号时,它将 twp 双引号之间的所有内容作为分隔符,因此将查找 )#/(: 而不是)或#或/或(或:

这是test_data的文件内容

[...srv01]$ cat test_data
JoeASA# sh failover | i \)\:|host
        This host: Primary - Active
                  admin Interface management (313.13.0.13): Normal (Monitored)
                  DMZ-FW Interface Inside (310.13.19.7): Normal (Not-Monitored)
                  DMZ-FW Interface Outside-Zone2 (912.168.119.7): Normal (Not-Monitored)
                  ENET Interface OUTSIDE(912.168.191.7): Normal (Not-Monitored)
                  ENET Interface dmarc (912.168.192.7): Normal (Not-Monitored)
                  GW Interface Extranet (912.168.23.27): Normal (Not-Monitored)
                  GW Interface Outside-Zone (912.168.123.27): Normal (Not-Monitored)
                  GW Interface management (331.1.1.47): Normal (Not-Monitored)
        Other host: Secondary - Standby Ready
                  admin Interface management (313.13.0.12): Normal (Monitored)
                  DMZ-FW Interface Inside (310.13.19.6): Normal (Not-Monitored)
                  DMZ-FW Interface Outside-Zone2 (912.168.119.6): Normal (Not-Monitored)
                  ENET Interface OUTSIDE(912.168.191.6): Normal (Not-Monitored)
                  ENET Interface dmarc (912.168.192.6): Normal (Not-Monitored)
                  GW Interface Extranet (912.168.23.26): Normal (Not-Monitored)
                  GW Interface Outside-Zone (912.168.123.26): Normal (Not-Monitored)
                  GW Interface management (331.1.1.46): Normal (Not-Monitored)          
SIMPLEASA1/sec/act#  sh failover | i \)\:|host
        This host: Secondary - Active
                  Interface Edge (912.168.22.17): Normal (Monitored)
                  Interface Inside (310.13.19.17): Normal (Monitored)
                  Interface EXT (912.168.50.17): Normal (Monitored)
                  Interface WIFI (912.168.11.17): Normal (Monitored)
        Other host: Primary - Standby Ready
                  Interface Edge (912.168.22.16): Normal (Monitored)
                  Interface Inside (310.13.19.16): Normal (Monitored)
                  Interface EXT (912.168.50.16): Normal (Monitored)
                  Interface WIFI (912.168.11.16): Normal (Monitored)                      
[..srv01]$ 
awk scripting
  • 1 个回答
  • 213 Views

Sidebar

Stats

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

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve