SED用任何字母顺序删除线路
我试图删除所有具有字母顺序的3个字符的线条,并使用SED删除任何简单的方法,而不是一堆模式线
sed -i '/abc/d
/bcd/d
....
/xyz/d' file.txt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能对您有用(gnu sed):
在第一行,在保留空间中引入一个字母字母。
在每行,附加字母内并使用三个字符回引用,将其比较字母。
如果有匹配,请删除该行,否则,仅打印第一行。
nb使用
-n
关闭隐式打印,因此只有在匹配失败时才打印出来。This might work for you (GNU sed):
On the first line, introduce a literal alphabet in the hold space.
On each line, append the alphabet and using a three character back reference, compare it the the alphabet.
If there is a match, delete the line, otherwise, print the first line only.
N.B. The use of the
-n
turns off implicit printing and thus only when a match fails is the line printed.使用您尝试的代码,请尝试以下
awk
代码,其中我们没有编写连续字母的所有组合。 IMHOawk
将在此处sed
在此处要高得多。说明: 整个
awk
程序的简单而详细的说明将是:的说明
开始
的块awk
程序:fs
)作为awk
中的所有行,以便每个行可以比较角色,以找出连续3个字母出现。split
awk
创建一个名为arr1
的数组,其中将所有字母(小字母)与的定界符,< /代码>在这里。
直到
num
的值(也可以写为26),因为始终固定字母的数量),在其中创建一个名为letters 具有索引为字母,其值将是其位置值(它们发生的数字,例如:对于
a
,它将为1
)。awk
程序的主要块的说明:运行 循环从第一个字段到
NF
基本上是当前行的所有字段。然后检查那里的条件(基本上检查当前字段和下一个字段是否在字母数组中出现,并检查其序列是否连续)。
如果满足所述所有条件,则打印电流和接下来的两个字段(基本上将打印3个字母)。
With your attempted code, please try following
awk
code, where we are not writing all combinations of continuous alphabets. IMHOawk
will be much efficient thensed
here.Explanation: Simple and detailed explanation for whole
awk
program would be:Explanation of
BEGIN
block ofawk
program:FS
) as NULL for all lines inawk
so that each character could be compared to find out 3 consecutive occurrences of letters.split
function ofawk
creating an array namedarr1
where splitting all alphabets(small letters) into it with delimiter of,
here.for
loop till value ofnum
(could be written as 26 also since number of alphabets are always fixed), where creating an array namedletters
which has index as alphabets and its value will be their place value(their number on which they occur, eg: fora
it will be1
).Explanation of main block of
awk
program:Running a
for
loop from 1st field to tillNF
all fields of current line basically.Then checking conditions there(basically checking if current field and next 2 fields are coming in letters array or not AND checking if their sequence is continuous or not).
If all conditions mentioned are met then printing current and next 2 fields(which will basically print 3 letters).