我期望这两个命令能够给出文件中包含字母的行数的相同数量:
grep -c '[A-Z,a-z]' archive_for_TO.050225
11873
grep '[A-Z,a-z]' archive_for_TO.050225 | wc -l
11859
文件太大,无法在此处发布,但我想知道是否有一些常见的情况/解释,说明为什么第二个命令显然比第一个命令少找到 14 行匹配行。例如,它是否表明文本文件在行分隔方式(字符返回或换行或两者)方面存在一些不一致之处?
我期望这两个命令能够给出文件中包含字母的行数的相同数量:
grep -c '[A-Z,a-z]' archive_for_TO.050225
11873
grep '[A-Z,a-z]' archive_for_TO.050225 | wc -l
11859
文件太大,无法在此处发布,但我想知道是否有一些常见的情况/解释,说明为什么第二个命令显然比第一个命令少找到 14 行匹配行。例如,它是否表明文本文件在行分隔方式(字符返回或换行或两者)方面存在一些不一致之处?
我正在尝试从 rsyslog 目录中删除垃圾文件(主要是许多空子目录或包含单个部分日志文件的子目录),但这些垃圾文件的名称各不相同,从看起来像被截断的单词到 10 个连字符。我认为最简单的方法是简单地查找我需要的任何东西,然后将其他一切都说成是垃圾,但这就是事情变得棘手的地方。
我需要保持
其余内容均可删除。
我考虑过只使用 grep 并排除与这些搜索词匹配的任何内容,然后将目录列表放入文本文件中,我可以将其用作要删除的输入列表,但我在输入主机名时遇到了问题,只要我说“vp*”,我什么也得不到。此外,我用来提取 IP 地址的字符串也在抓取日期。
我当前的搜索如下:
egrep -av 'HEAD|GET|version|putenv|id|default|POST|status|rsyslog_stats' |
grep -E "[^^][0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}" |
grep -av "vp*" |
grep -av "vd*"
如果有人有任何建议,我将不胜感激,因为如果我能让它工作,我可以每 15-30 分钟运行一次,并保持我的所有 rsyslog 侦听器目录清洁,并且不会将太多垃圾吸入我的 Splunk 索引中。
谢谢!
我正在使用 Arch Linux/Debian Linux,想要一个 ASCII txt 文件中的唯一“标识符”列表。以下是我想要缩减的数据片段:
... (Received from VRW): wind ...
... (Received from 1a00): air_ ...
... (Received from 5710): air_ ...
... (Received from ####): air_ ...
... (Received from 15d8): air_ ...
... (Received from ####): air_ ...
... (Received from 6e9e): baro ...
... (Received from 6e9e): volt ...
... (Received from 6e9e): wind ...
... (Received from 6e9e): air_ ...
由于文件很大且有大量重复的“标识符”,我只想输出唯一的标识符,以便输出如下所示:
... (Received from VRW): wind ...
... (Received from 1a00): air_ ...
... (Received from 5710): air_ ...
... (Received from ####): air_ ...
... (Received from 15d8): air_ ...
... (Received from 6e9e): baro ...
更好的做法是简单地列出唯一标识符,例如,,,15d8
等等。但我认为这会困难得多。6e9e
VRW
根据我以前尝试过的类似问题的建议:
grep "(Received from" datafile.txt
并得到了大量的标识符列表,其中大多数是重复的。
我也尝试过:
grep "(Received from" datafile.txt | sort -u
但不能说这是否有任何区别
我也尝试过:
parallel --tag --lb grep "Received from" {} | perl -ne '$seen{$_}++ or print;' ::: Data1.txt
这可能显示了我对这些问题的无知程度。
我经常使用 grep 在一堆文件夹中搜索一些文本,例如
grep --include='*.csv' -nRHI --color=always 'pattern' Folder1 Folder2
我想创建一个别名,例如
alias MyGrep="grep --include='*.csv' -nRHI --color=always Folder1 Folder2"
这样下次需要搜索时,我只需要
MyGrep 'pattern'
然而,这不起作用,因为搜索模式必须位于文件夹列表之前。
有没有可以使用别名的解决方法?
附言:我可以使用函数来代替,例如
function MyGrep() { grep --include='*.csv' -nRHI --color=always "$@" Folder1 Folder2;}
然后我可以跑
MyGrep pattern
但好奇是否可以通过别名解决。
我当前工作目录中的唯一文件名为test.txt
,其内容很简单:
This is a little test file.
grep -in * -e 'te.?t file'
没有匹配。?
有效:
grep -in * -e 'te.\?t file'
给出匹配疯狂:有了明星*
,事情就反过来了!
grep -in * -e 'te.*t file'
给出匹配*
不起作用:
grep -in * -e 'te.\*t file'
没有匹配为什么元字符在转义方面有?
区别*
对待?
我正在使用 tshark 动态解码 SMPP 协议,例如:
tshark -i any -V -Y smpp
这将实时显示每一个 SMPP 数据包,例如
Short Message Peer to Peer, Command: Submit_sm, Seq: 8475778, Len: 215
Length: 215
Operation: Submit_sm (0x00000004)
Sequence #: 8475778
Service type: (Default)
Type of number (originator): Alphanumeric (0x05)
Numbering plan indicator (originator): Unknown (0x00)
Originator address: \123\110\123\123\123aaa\123
Type of number (recipient): Unknown (0x00)
Numbering plan indicator (recipient): ISDN (E163/E164) (0x01)
Recipient address: 44123456789
.... ..00 = Messaging mode: Default SMSC mode (0x00)
..00 00.. = Message type: Default message type (0x00)
00.. .... = GSM features: No specific features selected (0x00)
Protocol id.: 0x43
有没有办法 grep 特定字段并仅输出这些字段?我尝试过:
tshark -i any -V -Y smpp | grep 'Operation|Type of number|Numbering plan indicator|Recipient address'
但它不起作用。我也尝试过:
tshark -i any -V -Y smpp -T fields -e 'smpp.dest_addr_ton' -e 'smpp.destination_addr'
但这只会输出值,而我想要输出类似的内容;
Type of number (recipient): Unknown (0x00)
Numbering plan indicator (recipient): ISDN (E163/E164) (0x01)
Recipient address: 44123456789
有人能给出建议吗?谢谢,卢卡斯
我想找到一个正则表达式或 perl 单行代码来检测两行连续的行,这些行包含相同的单词且顺序相同,但前导空格、尾随空格和单词之间的空格数量可能因行而异。正则表达式必须排除那些不匹配的行。
我使用这个正则表达式来检测相同的连续行,
^\s*(.*\S)(\s\n*\1)+$
当一行与另一行之间的空格量不同时,它会失败。
我有这个示例文本
Mary has a little lamb. The live near the mountain.
Mary has a little lamb. The live near the mountain.
vrfgrgru hfiughf iujhfuihgifughuir nuhuvhfug
Mary has a little lamb. The live near the mountain.
Mary has a little lamb. The live near the mountain.
Mary has a little lamb. The live near the mountain.
Mary has a little lamb. The live near the mountain.
fghurhg bojghirejyohi ndhguhyituhgi
gbshbsiughsf bnighriugh hurhgiurhgriu
Elvis was the greatest singer of all time. He has 18 number one in billboard charts.
Elvis was the greatest singer of all time. He has 18 number one in billboard charts.
sgfuhgdugusd vfjsng orjgoergo ejfio dsmnjn
fbdhsbfgdbhhbfhdsfdf
Mary has a little lamb. The live near the mountain.
Mary has a little lamb. The live near the mountain.
gsete gerttrb oijoijoijoijoijoiojo gogoerijoi
jhoiyrei erogjrijgtrio noifdo mkooi
This has been the coldest winter in the last 20 years.
This has been the coldest winter in the last 20 years.
无论行中有多少个空格,正则表达式都应该显示包含相同信息的连续行。
没有三个连续的并且匹配的行之间没有空行。
我正在阅读《灵活性软件设计 (SDF)》。练习 2.10 要求实现BRE 和 ERE 的翻译器。本书使用像 ERE 中grep
那样测试翻译的字符串(a.c)
来捕获类似abc
或 的内容adc
。
我使用 Arch Linux。
根据POSIX 规范9.3.3 BRE 特殊字符,\+
不受 BRE 支持。
但是man grep
“基本与扩展正则表达式”允许\+
使用默认选项 BRE。
BRE 对应于 ed 或historical grep 类型
不过ed
好像也支持\+
。
(已编辑:此段内容有误,因为启用选项 b 时,该网站根本不符合 BRE。(ab)
而不是\(ab\)
将匹配“ab”。)https://www.regexplanet.com/advanced/postgresql/index.html可以支持单纯的 BRE,但它有点慢,并且不能轻易地合并到 Scheme 代码中(我正在学习 SICP 和 SDF,所以我没有检查 Scheme 中的 Http 实现。上面的书使用 shell 来运行grep
)。我使用表达式a\+c
和输入abc
在启用选项 b 的情况下进行测试。它显示“regex_matches()”为“(none)”。
那么是否有一个正则表达式工具完全遵循BRE?
我正在寻找一个 grep 模式来查找某些代码中某个单词的出现情况(如果重要的话,是 Python)。
我想忽略较长的单词、字符串或注释中的出现的情况(如果可能的话)。
所以如果我有这个 Python 代码:
Variable = 5
print(Variable)
MyVariablePlusOne = 6
description = "This is Variable"
# I love Variable
Variable
我想要一种只匹配前两行的grep 方法。
我想在 Nginx 配置文件中 grep 一个 IP 地址。我想使用命令输入 IP 地址read
,但不想将输入存储在附加变量中。所以我想要这样的东西:
grep -ir $(read) /etc/nginx/
使用read
内部论证的正确方法是什么?