Unix script to remove the first line of a CSV file

后端 未结 4 1975
面向向阳花
面向向阳花 2020-12-23 11:21

I\'ve stated before I\'m not very good at scripting so what I have below copies files from a share directory to mine where I change their permissions. But then I wanted to r

相关标签:
4条回答
  • 2020-12-23 11:57

    I tried to delete the first row from a CSV, cd into the location and then the answer to the question didn't work for me, was getting

    sed: can't read 1d: No such file or directory

    So, I've used the following stated here by @amit

    sed -i '1d' filename.csv
    
    0 讨论(0)
  • 2020-12-23 11:57

    While looking for this answer I also found this other thread. There they say use tail, which doesn't actually make an modifications which is what the OP wanted. You could over course copy to a temporary file. Additionally, if you were going to stream the files into another tool, you could use 'tail' in a pipe and never have to write temporary files or write to disk.

    How can I remove the first line of a text file using bash/sed script?

    0 讨论(0)
  • 2020-12-23 12:17

    To create a new CSV file without the header, you can do the following:

    sed 1d file_with_header.csv > file_without_header.csv
    
    0 讨论(0)
  • 2020-12-23 12:19

    You can delete the first line of a file using sed:

    sed -i '' 1d file.csv
    

    If you just want to get the contents of the file without the first line, and without modifying the file, remove the -i '' flag:

    sed 1d file.csv
    

    Here 1d is the command to execute:

    • 1 => line where to act
    • d => delete

    So 1d means 'delete line 1'

    If you want to get the first line, you can use sed too:

    sed -n 1p file.csv
    

    Here p stands for 'print' or

    sed 1q file.csv
    

    (see William Pursell's comment)

    0 讨论(0)
提交回复
热议问题