简体   繁体   English

如何将所有行从文件传送到EOF?

[英]How to get all lines from a file to EOF?

I hope someone can help me to solve the problem. 我希望有人可以帮助我解决问题。 I found a lot of topics around reading files with sed, but nothing fits to my case. 我发现了很多有关使用sed读取文件的主题,但是没有什么适合我的情况的。 Maybe someone knows the solution to following situation: 也许有人知道以下情况的解决方案:

I have a logger that creates a file of a defined size (in my case 5MB). 我有一个记录器,它创建一个定义大小的文件(在我的情况下为5MB)。 The logger uses the file like a ring buffer and writes the logger information into this file. 记录器将文件用作环形缓冲区,并将记录器信息写入此文件。 The logger starts at, of course, line 1 and remarks the end with eof. 记录器当然从第1行开始,并以eof标记结尾。 In a hex editor, it looks like this: 在十六进制编辑器中,如下所示:

0d 3c 3c 3c 45 4f 46 3e 3e 3e 0d 20 20 20 20 20  .<<<EOF>>>.

Now I have two situations, an easy one and a complex one: 现在我有两种情况,一种是简单的,一种是复杂的:

  1. I need to print from start to the end identifier. 我需要从头到尾打印标识符。

  2. The most beautiful solution recognizes if after EOF are initial values (0x20), then print from line 1 to EOF. 最漂亮的解决方案是识别EOF之后是否为初始值(0x20),然后从第1行打印到EOF。 If there are values after end of line identifier then read all after EOF until file size and then from line one to EOF identifier. 如果在行尾标识符之后有值,则在EOF之后读取所有内容,直到文件大小,然后从第一行到EOF标识符。 This should print out all lines of this "ring buffer". 这应该打印出该“环形缓冲区”的所有行。 Is something like this possible? 这样的事情可能吗?

To solve (1) I tried some sed commands, eg: 解决(1)我尝试了一些sed命令,例如:

sed -e '1,$p' test.log > result.txt 

-> Aim: print everything from line 1 to EOF patter, but both files have the same size (in my case 5MB). ->目的:打印从第一行到EOF模式的所有内容,但是两个文件的大小相同(在我的情况下为5MB)。 It looks like $p refers to the real end of file and not to the EOF pattern. 看起来$ p指向文件的实际结尾而不是EOF模式。

sed -e '/EOF/,$d' test.log > result.txt 

-> Aim: print everything before EOF pattern, but result.txt has the size 0. ->目的:在EOF模式之前打印所有内容,但result.txt的大小为0。

Can anybody offer any hints or solutions to solve this? 有人可以提供任何提示或解决方案来解决此问题吗?

Using sed , I think you need two commands (and two scans of the file): 使用sed ,我认为您需要两个命令(以及文件的两次扫描):

logfile="…some-name…"
eofmark="<<<EOF>>>"

sed -n "/$eofmark/,\$ { /$eofmark/d; p; }" $logfile  # Read the tail material
sed -n "1,/$eofmark/  { /$eofmark/d; p; }" $logfile  # Read the head material

Using perl or awk , you could slurp the whole file into memory, and then print the tail part followed by the head part. 使用perlawk ,您可以将整个文件插入到内存中,然后打印尾部,然后打印头部。 For example, in awk : 例如,在awk

logfile="…some-name…"
eofmark="<<<EOF>>>"

awk "/$eofmark/"' {eofline = NR}
     {line[NR] = $0}
     END { for (i = eofline+1; i <= NR; i++) print line[i]
           for (i = 1; i < eofline; i++) print line[i]
     }' $logfile

This is feasible since reading a 5 MiB file into memory won't stress machines with gigabytes of main memory. 这是可行的,因为将5 MiB文件读入内存不会对具有千兆字节主内存的计算机造成压力。 If the file itself was gigabytes of data, you'd think twice about slurping it into memory, though scanning it twice would also be painful. 如果文件本身是千兆字节的数据,则将文件保存到内存时您会三思而后行,尽管扫描两次也会很痛苦。

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

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