Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix script to remove the first line of a CSV file

Tags:

vi

unix

csv

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 remove the first line in each of the CSV files — the headings — and then I wanted to get this to run every hour.

cd /users
scp dehpc14_Disk_Quota_Report.csv /h/u544835
scp dehpc14_User_Disk_Usage.csv /h/u544835
cd /h/u544835
chmod 755 dehpc14_Disk_Quota_Report.csv
chmod 755 dehpc14_User_Disk_Usage.csv

* insert delete csv first line here *

Can anyone help me with deleting the first line of the CSV file and getting this to run automatically every hour, for example, or direct me to something I might understand to get cracking with it?

like image 919
Zenaphor Avatar asked Mar 09 '12 11:03

Zenaphor


People also ask

How do I remove the first line from a csv file in Unix?

Here 1d is the command to execute: 1 => line where to act. d => delete.

How do I remove the first row in Unix?

sed is a common text processing utility in the Linux command-line. Removing the first line from an input file using the sed command is pretty straightforward. The sed command in the example above isn't hard to understand. The parameter '1d' tells the sed command to apply the 'd' (delete) action on line number '1'.

How do I delete a csv file in Unix?

Syntax: rm command to remove a file When rm command used just with the file names, rm deletes all given files without confirmation by the user. Warning: Be careful with filenames as Unix and Linux, by default, won't prompt for confirmation before deleting files.


3 Answers

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)

like image 96
Carlos Campderrós Avatar answered Oct 22 '22 23:10

Carlos Campderrós


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
like image 36
Tiago Martins Peres Avatar answered Oct 22 '22 23:10

Tiago Martins Peres


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

sed 1d file_with_header.csv > file_without_header.csv
like image 33
Nic Scozzaro Avatar answered Oct 22 '22 23:10

Nic Scozzaro