Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete the first line in a file?

I can't search for a particular string, since they're all very similar, but I'd like something simple to chop out the first 4 lines in a file.

They're all variable length too. I've had a think about perl, and it all seems harder than I thought, but I'd like to do it in Perl, AWK or a shell command if possible.

Does anybody have a simple way of doing this?

like image 564
Soop Avatar asked Apr 16 '10 10:04

Soop


People also ask

How do I remove the first line from a file?

Using the sed Command 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 specific line?

Delete lines or connectorsClick the line, connector, or shape that you want to delete, and then press Delete. Tip: If you want to delete multiple lines or connectors, select the first line, press and hold Ctrl while you select the other lines, and then press Delete.

How do I remove the first line of a text file in Java?

Scanner fileScanner = new Scanner(myFile); fileScanner. nextLine(); This will return the first line of text from the file and discard it because you don't store it anywhere.

How do you remove one line from a file in Linux?

To delete a line, we'll use the sed “d” command. Note that you have to declare which line to delete. Otherwise, sed will delete all the lines.


2 Answers

tail -n+2 filename

Will skip the first line in filename and output it to stdout. The -n+2 option means to start outputting lines beginning with the second line.

Of course you can substitute 2 with whatever number you need (your title and actual question content say first and fifth, respectively).

like image 126
Mark Rushakoff Avatar answered Sep 19 '22 06:09

Mark Rushakoff


sed is the simplest. To delete the first line of a file, do:

sed '1d' file.txt

Or, to remove the first four lines, do:

sed '1,4d' file.txt
like image 41
Vicky Avatar answered Sep 18 '22 06:09

Vicky