How to move first N bytes from text file to another text file

五迷三道 提交于 2019-12-11 14:55:26

问题


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.

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.


回答1:


I am not sure what you want: Your headline says move first N byte to another file, your text says you want to delete the first N bytes

tail -c +N log.txt

Will output everything after the first N bytes, so this will answer you text question. Moving them to another file (as your headline requests) you have to do

head -c N log.txt > other.file



回答2:


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

so you can do something like tail -c +N log.txt.file

 where N is the Number of bytes to delete

Example :

 [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)

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


来源:https://stackoverflow.com/questions/25177284/how-to-move-first-n-bytes-from-text-file-to-another-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!