Remove specific lines from a large text file in python

前端 未结 3 1977
情歌与酒
情歌与酒 2020-12-17 07:01

I have several large text text files that all have the same structure and I want to delete the first 3 lines and then remove illegal characters from the 4th line. I don\'t w

3条回答
  •  一个人的身影
    2020-12-17 07:51

    As wim said in the comments, sed is the right tool for this. The following command should do what you want:

    sed -i -e '4 s/(dB)//' -e '4 s/Best Unit/Best_Unit/' -e '1,3 d' yourfile.whatever
    

    To explain the command a little:

    -i executes the command in place, that is it writes the output back into the input file

    -e execute a command

    '4 s/(dB)//' on line 4, subsitute '' for '(dB)'

    '4 s/Best Unit/Best_Unit/' same as above, except different find and replace strings

    '1,3 d' from line 1 to line 3 (inclusive) delete the entire line

    sed is a really powerful tool, which can do much more than just this, well worth learning.

提交回复
热议问题