如何将前N个字节从文本文件移动到另一个文本文件 [英] How to move first N bytes from text file to another text file
问题描述
我遇到了一个极端问题,我能想到的所有解决方案都很复杂。根据我的UNIX / Linux经验,必须有一种简单的方法。
I've got an extreme problem, and all of the solutions I can imagine are complicated. According to my UNIX/Linux experience there must be an easy way.
我要删除log.txt.file中文件的前n个字节足够长。好吧,我敢肯定有人会为我提供一个我无法想象的简单的解决方案。
I want to delete the first n bytes of file in log.txt.file is long enough. Well, I'm sure somebody will deliver me a suprisingly easy solution I just can't imagine.
推荐答案
将 tail 与 -c 选项一起使用怎么办
What about using tail with it's -c option
from man:
-c, --bytes=K
output the last K bytes; alternatively, use -c +K to output bytes starting with the Kth of each file
像
tail -c + N log.txt.file
so you can do something like tail -c +N log.txt.file
where N is the Number of bytes to delete
示例:
[prompt]$ cat file
ABCDEFGH
[prompt]$ tail -c +2 file //output bytes starting from the second byte
BCDEFGH
[prompt]$ tail -c +3 file //output bytes starting from the third byte
CDEFGH
[prompt]$ tail -c +5 file //output bytes starting from the fifth byte
EFGH
[prompt]$ ls -l
-rw-r--r-- 1 user users 9 2014-08-07 10:22 file // 9 bytes
[prompt]$ tail -c +S file >> file2 //this will copy file content to file2 escaping the first 4 bytes
[prompt]$ ls -l file2
-rw-r--r-- 1 user users 5 2014-08-07 10:29 file2 // 5 bytes (9 - first 4 bytes)
我不得不提到您可以也使用-bytes 选项从文件中获取最后N个字节。
i have to mention that you can use also --bytes option to get the last N bytes from a file.
[prompt]$ tail --bytes=5 file //print the last 5 bytes
EFGH
这篇关于如何将前N个字节从文本文件移动到另一个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!