MMa*_*udi 1 bash shell awk sed
我尝试使用sed命令解析文件,并仅在下一行分别保留重复的行。
(写在名为的文件中test)
line
line
line
line1
line
line3
line1
line2
line2
line
Run Code Online (Sandbox Code Playgroud)
line
line
line2
Run Code Online (Sandbox Code Playgroud)
cat test | sed -rn '$!N; /^(.*)\n\1/P; D'
Run Code Online (Sandbox Code Playgroud)
line
line
line
line
line2
Run Code Online (Sandbox Code Playgroud)
我想知道此命令有什么问题吗?为什么还要再增加2个line?
如果awk可以,请尝试以下操作。
awk 'prev==$0 && prev{print} {prev=$0}' Input_file
Run Code Online (Sandbox Code Playgroud)
要么
awk 'prev==$0 && prev; {prev=$0}' Input_file
Run Code Online (Sandbox Code Playgroud)
说明:现在为上述代码添加说明。
awk ' ##Starting awk program here.
prev==$0 && prev{ ##Checking condition if prev variable is equal to current line and prev is NOT NULL.
print ##Printing the current line then, as per OP request.
} ##Closing BLOCK for prev==$0 && prev condition here.
{
prev=$0 ##Setting variable prev to current line.
}
' Input_file ##Mentioning Input_file name here(which is getting passed to awk).
Run Code Online (Sandbox Code Playgroud)