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
    • 最新
    • 标签
主页 / ubuntu / 问题

问题[regex](ubuntu)

Martin Hope
CryptoTrader
Asked: 2021-10-21 10:08:14 +0800 CST

匹配我有足够能力负担的所有物品。简单的正则表达式

  • -2

参加一个自我指导的 linux 教程,我一直对此感到困惑

我的输出一直包括“时钟 15”,请帮忙

#!/bin/sh
#comment single RegEx to match all of the items that you have enough rupees 
sed '1d' hw0207.txt | grep -v [2-9][0-9]
#comment grep will filter all numbers greater than 12, -v represents not
#comment first sed was to remove first line

正则表达式匹配方向

我正在尝试编写一个正则表达式来匹配您有足够卢比的所有项目。不要只匹配线,你应该匹配你能负担得起的线,这样如果价格发生变化,答案仍然是正确的。你只有 12 卢比,如果你想买更多的东西,你需要变得更富有一点。

Input file (hw0207.txt) Expected output of script
item cost
lamp oil 5
rope 10
clock 15
bombs 20
regex
  • 1 个回答
  • 139 Views
Martin Hope
CryptoTrader
Asked: 2021-10-20 16:58:19 +0800 CST

正则表达式匹配访问某个端口和特定数据包的行

  • 0

Linux 新手,我可以让它工作的唯一方法是使用 awk 命令,不幸的是,主要方向指定不使用 awk。

这就是我得到的

#!/bin/sh
#comment Write a single RegEx to match the lines that access port 22 and only those packets
grep '\s22\s' hw0206.txt | awk {'print $4'}
#comment grep returns the whole line with matched string
#comment \s22\s regular expression to match any string containing 22 preceded or succeeded by white space

指示

编写一个正则表达式来匹配访问端口 22 的行并且只匹配那些数据包,然后返回 IP 地址

Input file (hw0206.txt) Expected output of script
date time protocol ip-address port packet-size
2022-02-21 19:22:19 TCP 22.101.2.24 22 24
2018-22-22 02:25:12 UDP 10.221.7.22 2135 222
2200-05-22 22:26:22 UDP 22.122.6.62 2160 22
2012-22-20 15:43:22 TCP 10.121.7.222 22 122
1228-02-10 02:22:02 UDP 22.102.2.62 2089 22 
date time protocol ip-address port packet-size
2022-02-21 19:22:19 TCP 22.101.2.24 22 24
2018-22-22 02:25:12 UDP 10.221.7.22 2135 222
2200-05-22 22:26:22 UDP 22.122.6.62 2160 22
2012-22-20 15:43:22 TCP 10.121.7.222 23 122
1228-02-10 02:22:02 TCP 22.102.2.62 22 22
2100-05-25 21:26:22 UDP 22.112.63.62 2122 22
regex
  • 1 个回答
  • 284 Views
Martin Hope
Mario Palumbo
Asked: 2020-12-21 02:24:37 +0800 CST

bash perl \z 不起作用

  • 3

我有这个文本文件或 sdout:

text1
text2
text1
text2
text1
text2

我有这个代码:

perl -pe "s/text2\n\z/text3/s" text.txt #Note the modifier "/s"

我和 \z 希望他明白这是 eof 之前的最后一行,因此我希望:

text1
text2
text1
text2
text1
text3

但相反,它返回:

text1
text3text1
text3text1
text3 #without final newline

我究竟做错了什么?

bash regex perl
  • 3 个回答
  • 201 Views
Martin Hope
Mario Palumbo
Asked: 2020-11-25 07:58:27 +0800 CST

回复:当我在 Perl 的匹配运算符中插入变量时,如何转义元字符?

  • 0

链接

在 Perl 的匹配运算符中插入变量时,如何转义元字符?

没有帮助我。

我尝试过使用 Python:

import os
line='ID_SN=02      MS=DC:A6:32:7E:74:08    S=*     TS=24/11/2020 11:02:30 CET      N=IMC2500       MD=7F:7A:25:43:8F:44    T=LE    P=90@'
cmd1="perl -sni -e 'print unless /^\Q$regex\E$/' -- -regex=%s device_scan.txt" % repr(line)
os.system(cmd1)

但是\Q......\E也阻止解释,\t但我只需要解释。所以没有行被删除。

这似乎是一个相当复杂的问题,但我希望你能帮助我。

python bash regex perl
  • 3 个回答
  • 94 Views
Martin Hope
Mario Palumbo
Asked: 2020-11-07 02:39:31 +0800 CST

bash perl“和”运算符

  • 0

输出:

apple text1
peach text2
banana text3
melon text4

对于删除以“apple”或“banana”开头的行,我输入:

perl -pe 's/^apple.*\n|^banana.*\n//g'

并且输出是正确的:

peach text2
melon text4

但我也想删除最终的“木瓜”或“芒果”。为了实现这一点,我应用了德摩根定律:

perl -pe 's/^(?!peach).*\n&^(?!melon).*\n//g'

但是什么都没有被删除,因为 while "|" 代表“或”,“&”不起作用。
在 bash perl 命令中,什么符号代表“和”?

regex perl
  • 1 个回答
  • 80 Views
Martin Hope
Mario Palumbo
Asked: 2020-11-06 06:04:15 +0800 CST

bash --> perl 命令:只打印替换的文本

  • 4

我有文件“test.txt”,其中包含:

Val1 = '59'
Val2 = '76'
Val3 = '42'
Val4 = '53'

我用这个命令:

perl -pe "s/^Val2 = '(.*)'/\1/" test.txt

我想要:

76

但我得到:

Val1 = '59'
76
Val3 = '42'
Val4 = '53'
bash regex perl
  • 2 个回答
  • 590 Views
Martin Hope
user1046658
Asked: 2020-09-27 12:07:26 +0800 CST

grep 是否区分变量和 $ 正则表达式?

  • 1

我想 grep 查找具有 string 的变量test,但我也想确保它只会找到它test是否是一行中唯一的单词。

所以,我尝试了这个:

string=test
echo "test" | grep "$string$"

我想知道是否grep知道第一部分$string(在 grep " $string $" 中)要求查找字符串test,而最后一部分$(在 grep "$string $ " 中要求查找test,但是行中不应该有任何内容test。grep能够区$string分为变量,并且只能$作为行尾的正则表达式,还是我必须使用某种方式来区分它们?

command-line regex grep
  • 1 个回答
  • 3129 Views
Martin Hope
kramer
Asked: 2020-09-04 01:19:43 +0800 CST

如何仅对 USB 密钥进行排序?

  • 1

我在 Ubuntu 20.04

如果我只想对 USB 键进行排序以得到这样的输出怎么办?

USB 密钥名称/大小 Gb/USB 密钥的路径

我试过这个,但我有硬盘驱动器和 USB 密钥。

df -ah | awk '/media/{print $1, $2, $6}'

或者这个更好

ls -l /dev/disk/by-id | grep [a-z]$ | awk '/usb/{print $9, $11}'

这给了

usb-Intuix_U3_0DF069605361E946-0:0 ../../sdg
usb-Kingston_DataTraveler_2.0_60A44C3FAD9EFE41E98E256A-0:0 ../../sdf
usb-_USB_DISK_3.0_070B84A2C7C19B89-0:0 ../../sdh
usb-Verbatim_STORE_N_GO_1230000000003CFD-0:0 ../../sdi
regex 20.04
  • 2 个回答
  • 79 Views
Martin Hope
Mario Ishac
Asked: 2020-07-27 21:28:20 +0800 CST

如何根据行开始将文件拆分为两个?

  • 6

我有这个文件1.txt:

-e a
b
-e c

d
-e e
f

我想将其拆分为以下两个文件。

2.txt

-e a
-e c
-e e

3.txt

b
d
f

where2.txt包含以 开头的所有行-e,并3.txt包含所有其他行。可以忽略或保留额外的换行符(例如原始中间的额外换行符),并且顺序无关紧要。

我尝试过使用split,但它看起来不允许我使用模式进行拆分(而不是每个拆分文件的固定行数)。

command-line regex split
  • 4 个回答
  • 1051 Views
Martin Hope
mongotop
Asked: 2020-07-25 16:06:33 +0800 CST

使用每个 JSON 对象值的正则表达式创建键值对并将其附加到 JSON 对象文件

  • 2

我有一个大文件,其中包含 json 对象,每个对象都在一个新行中。

文件示例

{"Name" :"%Hana-29-Mrs-Smith","job":"engineer"}
{"Name" :"%Mike-31-Mr-Larry","job":"marketing"}
{"Name" :"%Jhon-40-Mr-Doe","job":"engineer"}

期望的输出:

{"Name" :"%Hana-29-Mr-Smith", "f_nams":"Hana", "age":29, "title":"Mrs", "l_name":"Smith","job":"engineer"}
{"Name" :"%Mike-29-Mr-Larry", "f_nams":"Mike", "age":31, "title":"Mr", "l_name":"Larry","job":"marketing"}
{"Name" :"%Jhon-29-Mr-Smith", "f_nams":"Jhon", "age":40, "title":"Mr", "l_name":"Doe","job":"engineer"}
command-line regex powershell json
  • 2 个回答
  • 1389 Views

Sidebar

Stats

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

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve