我需要使用 awk 搜索关键字,但我想执行不区分大小写(不区分大小写)的搜索。
我认为最好的方法是将搜索词(“关键字”)和 awk 同时阅读的目标行大写。从这个问题我如何使用toupper
以全部大写打印,但我不知道如何在匹配中使用它,因为该答案仅显示打印并且不会将大写文本留在变量中。
这是一个例子,给定这个输入:
blablabla
&&&Key Word&&&
I want all
these text and numbers 123
and chars !"£$%&
as output
&&&KEY WORD&&&
blablabla
Run Code Online (Sandbox Code Playgroud)
我想要这个输出:
I want all
these text and numbers 123
and chars !"£$%&
as output
Run Code Online (Sandbox Code Playgroud)
这是我所拥有的,但我不知道如何添加toupper
:
awk "BEGIN {p=0}; /&&&key word&&&/ { p = ! p ; next } ; p { print }" text.txt
Run Code Online (Sandbox Code Playgroud) 我想从外部文件 grep 链接example.txt
。
example.txt
包含:
(https://example.com/pathto/music.mp3)music.mp3
Run Code Online (Sandbox Code Playgroud)
编码:
egrep -o -m1 '(https)[^'\"]+.mp3' example.txt
Run Code Online (Sandbox Code Playgroud)
输出:
https://example.com/pathto/music1.mp3)music.mp3
Run Code Online (Sandbox Code Playgroud)
当我运行 grep 时,它将最后一个 .mp3 检测为输出结束,而我只需要它在第一次出现后结束。如何在找到第一个模式后告诉 grep 停止?
我想要的输出:
https://example.com/pathto/music.mp3
Run Code Online (Sandbox Code Playgroud)
我只想提取以开头https
和结尾的任何字符串mp3
我有 Lubuntu 16.04。我的问题类似于这个问题:
sudo apt-get update
Get:1 http://it.archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
Hit:2 http://ppa.launchpad.net/fkrull/deadsnakes/ubuntu xenial InRelease
Hit:3 http://it.archive.ubuntu.com/ubuntu xenial-updates InRelease
Hit:4 http://it.archive.ubuntu.com/ubuntu xenial-backports InRelease
Hit:5 http://www.bchemnet.com/suldr debian InRelease
Hit:6 http://archive.canonical.com xenial InRelease
Get:7 http://security.ubuntu.com/ubuntu xenial-security InRelease [93,3 kB]
Fetched 340 kB in 6s (52,6 kB/s)
Reading package lists... Done
W: There is no public key available for the following key IDs:
FB510D557CC3E840
Run Code Online (Sandbox Code Playgroud)
提供的答案均无效:
根据这个问题的答案#1:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys FB510D557CC3E840
Executing: /tmp/tmp.oqit2axEyE/gpg.1.sh --keyserver keyserver.ubuntu.com --recv-keys …
Run Code Online (Sandbox Code Playgroud)考虑一下text.txt
这个内容:
blablabla
&&&key word&&&
I want all
these text and numbers 123
and chars !"£$%&
as output
&&&key word&&&
blablabla
Run Code Online (Sandbox Code Playgroud)
我想实现一个正则表达式,用awk
它获得以下输出:
I want all
these text and numbers 123
and chars !"£$%&
as output
Run Code Online (Sandbox Code Playgroud)
我正在使用这个表达式:
awk "$1 ~ /^[&&&key word&&&].[&&&key word&&&]/" test.txt
Run Code Online (Sandbox Code Playgroud)
但结果不是我所期望的:
&&&key word&&&
&&&key word&&&
Run Code Online (Sandbox Code Playgroud)
我道歉,因为这可能不是一个好问题。我已经从这里和 stackoverflow 尝试了很多解决方案,我阅读了一些教程,但仍然无法解决。如果你也能给我一个关于这个主题的完整教程,我将不胜感激。
编辑:按照建议使用 $0,我得到了更好的结果:
awk "$0 ~ /[&&&key word&&&]/,/[&&&key word&&&]/" test.txt
Run Code Online (Sandbox Code Playgroud)
结果是:
&&&key word&&&
I want all
these text and numbers 123
and chars !"£$%&
as result
&&&key …
Run Code Online (Sandbox Code Playgroud)