简体   繁体   English

删除一行和它之前的一行

[英]removing a line and the line before it

i'm trying to remove the lines containing literal,32, and the line BEFORE/ABOVE it that should always contain,34,我正在尝试删除包含文字 32 的行,以及应该始终包含 34 的之前/上方的行,

files example:
dd,34,dd 10:00 game1
dd,32,dd 10:01 game1
dd,34,dd 12:30 game2
dd,31,dd 12:32 game2 
dd,34,dd 13:54 game3
dd,31,dd 13:55 game3
dd,34,dd 15:00 game1
dd,32,dd 15:00 game1

#note: there's a few thousand of these lines in the file #note:文件中有几千行

I've tried using grep grep -v -B1 ',32,' file1 > file2我试过使用 grep grep -v -B1 ',32,' file1 > file2

file2 should be all lines from file1 except for the lines containing,32, and the line before,32, doesnt work as intented file2 应该是 file1 的所有行,但包含 32 的行和之前的行 32 不按预期工作

sed "/\,32\,/,+1d" rp1 > rep2 sed "/\,32\,/,~1d" rp1 > rep2 deletes lines in a different order than intended. sed "/\,32\,/,+1d" rp1 > rep2 sed "/\,32\,/,~1d" rp1 > rep2以与预期不同的顺序删除行。 The amounts of lines containing,34, match the lines containing the rest of the symbols.包含 34 行的数量与包含 rest 个符号的行匹配。 As if it deletes the,32, line and the line AFTER instead of BEOFRE.就好像它删除了 32 行和 AFTER 行而不是 BEOFRE 行。

OUTPUT AFTER USING SED command from above: OUTPUT 使用上面的 SED 命令后:

dd,34,dd 10:00 game1
dd,31,dd 12:32 game2 
dd,34,dd 13:54 game3
dd,31,dd 13:55 game3
dd,34,dd 15:00 game1

DESIRED OUTPUT:所需 OUTPUT:

dd,34,dd 12:30 game2
dd,31,dd 12:32 game2 
dd,34,dd 13:54 game3
dd,31,dd 13:55 game3

To get the desired output you need to understand The Concept of 'Hold space' and 'Pattern space' in sed要获得所需的 output,您需要了解 sed 中“保留空间”和“模式空间”的概念
Please refer the above URL to get an understanding.请参考以上URL了解。

You can use the following sed command to get the desired output:您可以使用以下sed命令获取所需的 output:

sed -n '/,32,/{s/.*//;x;d;};x;p;${x;p;}' inputFileName | sed '/^$/d'

The above command will delete all the lines matching ,32, pattern and the line exactly above/before it.上面的命令将删除所有与,32, pattern 匹配的行以及它上面/之前的行。

The above command is storing every line in a buffer called as hold space , when sed encounters the pattern which is ,32, it deletes the content of pattern space ie current line as well as hold space ie the previous line.上面的命令将每一行存储在一个称为保持空间的缓冲区中,当sed遇到模式,32,它会删除模式空间的内容,即当前行以及保持空间,即上一行。

Hope this answers the question.希望这能回答问题。

Using any awk plus tac:使用任何 awk 加上 tac:

$ tac file | awk '/,32,/{c=2} !(c&&c--)' | tac
dd,34,dd 12:30 game2
dd,31,dd 12:32 game2
dd,34,dd 13:54 game3
dd,31,dd 13:55 game3

If you wanted to delete blocks of, say, 5 lines instead of blocks of 2 lines, you'd just change c=2 to c=5 .如果您想删除 5 行块而不是 2 行块,您只需将c=2更改为c=5

See Printing with sed or awk a line following a matching pattern for an explanation for that awk script and similar idioms.请参阅使用 sed 或 awk 匹配模式后的一行打印awk 脚本和类似习语的解释。

This might work for you (GNU sed):这可能对你有用(GNU sed):

sed -n 'N;/\n.*,32,/d;P;D' file

Open a two line window.开通二线window。

If the second line contains ,32, , delete both lines.如果第二行包含,32, ,则删除这两行。

Otherwise, print/delete the first and re-establish the two line window.否则,打印/删除第一行并重新建立两行 window。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM