I need to delete string from position X to position Y on each line in a text file

后端 未结 3 1901
轻奢々
轻奢々 2021-01-24 12:57

I have a huge flat file 100K records each spanning 3000 columns. I need to removed a segment of the data fay starting position 300 to position 500 before archiving. This is sens

3条回答
  •  一整个雨季
    2021-01-24 13:45

    Using sed:

    sed -r -i.bak 's/(.{299}).{200}/\1/' file
    

    The -r option enables extended regex. If you need to make it portable you can remove that option by escaping braces and curlies. The -i option makes changes in-places. I have put an extension .bak to safeguard from any mess up. You can remove it if you don't need to maintain the backup of original.

    For solution, we just capture the first 299 characters in a capture group and seek the next 200 we need to remove. We substitute this entire line with our captured group.

提交回复
热议问题