Linux delete first line in file

How to delete lines from a file using sed command

sed command stands for Stream Editor. It is used to perform basic text transformations in Linux.

sed is one of the important command, which plays major role in file manipulations. It can be used for many purposes some of which are listed below:

  • To delete or remove specific lines which matches with given pattern.
  • To remove a particular line based on position in a file.
  • Removing lines using regular expressions.

There are 27 examples listed in this article, which will help you to become a master in sed command.

If you could memorize these commands, it will save a lot of your time when you have the requirement to perform various admin tasks in your projects.

Also, check this article on how you can use the sed command to replace a matching string in a file.

Note: Since this is a demonstration article, we use sed command without the -i option, which is similar to the “dry run” option, and will display the actual output without making any changes in the file.

But, if you would like to remove the lines from the source file in real environment then use the -i option with sed command.

To demonstrate sed command, we have created the ‘sed-demo.txt’ file and added the following contents with line numbers for better understanding.

Part-I) Removing lines based on a position in the file

The first part shows how to use sed command to delete lines based on a position in the file.

1) How to delete first line from a file?

To delete the first line from a file, use the following syntax:

Syntax:

  • N – Denotes ‘Nth’ line in a file
  • d – Indicates the deletion of a line.

The below sed command removes the first line from the ‘sed-demo.txt’ file:

2) How to delete last line from a file?

If you would like to delete the last line from a file, use the following syntax ( $ denotes the last line of a file).

The below sed command removes the last line from the ‘sed-demo.txt’ file:

The following sed command removes the first and last line from the file:

3) Deleting a particular line from a file

In this example, we will delete the third line from the ‘sed-demo.txt’ file using sed command as shown below. Similarly, any line can be removed by entering the line number instead of the number 3.

4) Deleting range of lines

The sed command removes any range of given lines from a file. We need to enter the ‘minimum’ and ‘maximum’ line numbers. The below example removes lines ranging from 5 to 7.

Читайте также:  Моргает монитор компьютера причины windows 10 кабель vga

5) How to remove multiple lines

The sed command is capable of removing a set of given lines.

In this example, the following sed command will remove 1st , 5th , 9th , and the last line.

5.a) Deleting lines other than the specified range

Use the following sed command to remove all the lines from the file, except the specified range of lines:

Details;

  • ! – Negation operator is used to keep the specified lines

Use the below command to delete all lines other than the first line:

Use the below command to delete all lines except the last line:

6) Deleting empty or blank lines

The following sed command will remove the empty or blank lines from ‘sed-demo.txt’ file.

Part-II) Removing lines based on pattern match

The second part shows, how to use sed command to remove lines in a file that match a given pattern.

7) Removing lines that contain a pattern

The following sed command will remove the lines which match the System pattern in ‘sed-demo.txt’ file .

8) Deleting lines that contain one of several strings

The following sed command removes the lines which match with the System or Linux pattern from the file ‘sed-demo.txt’:

9) Deleting lines that begin with specific character

The following sed command will remove all the lines that begin with a given character. To demonstrate this, I have created another file called ‘sed-demo-1.txt’ with following contents:

The following sed command removes all the lines that start with character R :

The following sed command will remove the lines that begin with the character either R or F :

The following sed command will remove all the lines that starts with character L and ends with System string :

10) Removing lines that end with specified character

The following sed command removes all the lines that end with character m :

The following sed command removes all the lines that ends with character either x or m :

11) Deleting all lines that start with uppercase

Use the following sed command to remove all the lines that start with an Uppercase letter:

12) Deleting a matching pattern lines with specified range

The below sed command removes the pattern Linux only if it is present in the lines from 1 to 6 :

12.a) Deleting only the last line if it contains a given pattern

The below sed command only removes the last line if it contains the pattern “openSUSE

13) How to delete pattern matching lines and also the next Line?

Use the following sed command to delete the line which matches the pattern System and the subsequent line in the file as well.

13.a) Deleting lines starting from a pattern till the last line

The below sed command removes the line that matches the pattern “CentOS”, and also deletes all the subsequent lines till the end of the file:

14) Deleting lines that contains Digits/Numbers

The below sed command removes all the lines that contains ‘digits’:

The below sed command removes all the lines which only begins with digits:

The below sed command removes all the lines which ends with digits:

15) Deleting lines that contains Alphabetic Characters from a file

The below sed command removes all the lines that contains any alphabetic characters.

Closing Notes

In this guide, we have shown you several examples to delete lines from a file using the sed command.

If you have any questions or feedback, feel free to comment below.

Источник

How do I delete the first n lines of an ascii file using shell commands?

I have multiple files that contain ascii text information in the first 5-10 lines, followed by well-tabulated matrix information. In a shell script, I want to remove these first few lines of text so that I can use the pure matrix information in another program. How can I use bash shell commands to do this?

If it’s any help, I’m using RedHat and an Ubuntu linux systems.

7 Answers 7

As long as the file is not a symlink or hardlink, you can use sed, tail, or awk. Example below.

You can also use sed in-place without a temp file: sed -i -e 1,3d yourfile . This won’t echo anything, it will just modify the file in-place. If you don’t need to pipe the result to another command, this is easier.

Читайте также:  Каталоги linux организация файловой системы

sed -i ‘1,3d’ file.txt

This deletes first 3 line from file.txt.

1GB left of space, this solution won’t work as creates a tmp file

If the tabulated lines are the ones that have a tab character:

( ␉ being a literal tab character) or equivalently

In a bash/ksh/zsh script, you can write $’\t’ for a tab, e.g. grep $’\t’ or sed -n $’/\t/p’ .

If you want to eliminate 10 lines at the beginning of the file:

(note that it’s +11 to eliminate 10 lines, because +11 means “start from line 11” and tail numbers lines from 1) or

On Linux, you can take advantage of GNU sed’s -i option to modify files in place:

Or you can use a shell loop and temporary files:

Or if you don’t want to modify the files in place, but instead give them a different name:

Источник

Remove first N lines of a file in place in unix command line

I’m trying to remove the first 37 lines from a very, very large file. I started trying sed and awk, but they seem to require copying the data to a new file. I’m looking for a «remove lines in place» method, that unlike sed -i is not making copies of any kind, but rather is just removing lines from the existing file.

Here’s what I’ve done.

Both of these seem to do a full copy. Is there any other simple CLI that can do this quickly without a full document traversal?

4 Answers 4

There’s no simple way to do inplace editing using UNIX utilities, but here’s one inplace file modification solution that you might be able to modify to work for you (courtesy of Robert Bonomi at https://groups.google.com/forum/#!topic/comp.unix.shell/5PRRZIP0v64):

The final file should be $bytes bytes smaller than the original (since the goal was to remove $bytes bytes from the beginning), so to finish we must remove the final $bytes bytes. We’re using conv=notrunc above to make sure that the file doesn’t get completely emptied rather than just truncated (see below for example). On a GNU system such as Linux doing the truncation afterwards can be accomplished by:

For example to delete the first 5 lines from this 12-line file

First use dd to remove the target 5 lines (really «$bytes» bytes) from the start of the file and copy the rest from the end to the front but leave the trailing «$bytes» bytes as-is:

and then use truncate to remove those leftover bytes from the end:

If we had tried the above without dd . conv=notrunc :

See the google groups thread I referenced for other suggestions and info.

Источник

Remove First n Lines of a Large Text File

I need to remove the first 42 lines of a 2GB SQL dump.

I know I can view the first lines using:

But is there anyway to edit or remove them?

8 Answers 8

If you want to just view the lines from the 43rd on you can use

The + sign is important — without it, tail will print the last 43 lines instead. Alternatively with ‘sed’

If you want to really delete the first 42 lines from the original file then you can make sed make the change inplace with the -i option

700MB. Takes awhile for the file to load, though.

This seems to be the easiest:

Remove lines 1-42 from test.sql and save as test2.sql

tail -n +43 dump.sql > dump_new.sql

You can use Vim in Ex mode:

1 move to first line

42 select 42 lines

x save and close

Because of sed discrepancies across Linux and Mac, I resolved to use tail -n +43 dump.sql > new.sql format.

Just to add this. If you’re on a mac you need to add the backup extension. Answer from this post.

Sorry, I can’t give you actual code right now. However, try looking at something along the lines of

Читайте также:  Дигма смарт лайф для windows

What this should do (once properly formatted) is count the number of lines in the file (wc -l), subtract 44 from it (-44) and then print out everything starting with the 45th line in the file.

Источник

How do I delete the first n lines and last line of a file using shell commands?

I have a file named Element_query containing the result of a query :

I want to delete 1st line and last line using shell command.

9 Answers 9

How it works :

  • -i option edit the file itself. You could also remove that option and redirect the output to a new file or another command if you want.
  • 1d deletes the first line ( 1 to only act on the first line, d to delete it)
  • $d deletes the last line ( $ to only act on the last line, d to delete it)

Going further :

  • You can also delete a range. For example, 1,5d would delete the first 5 lines.
  • You can also delete every line that begins with SQL> using the statement /^SQL> /d
  • You could delete every blank line with /^$/d
  • Finally, you can combine any of the statement by separating them with a semi-colon ( statement1;statement2;satement3;. ) or by specifying them separately on the command line ( -e ‘statement1’ -e ‘statement 2’ . )

head; head

With the above you can specify the first number of lines to strip off of the head of the output w/ the first head command, and the number of lines to write to outfile with the second. It will also typically do this faster than sed — especially when input is large — despite requiring two invocations. Where sed definitely should be preferred though, is in the case that is not a regular, lseekable file — because this will typically not work as intended in that case, but sed can handle all output modifications in a single, scripted process.

With a GNU head you can use the — negative form for [num] in the second command as well. In which case the following command will strip first and last lines from input:

OR with a POSIX sed :

Say, for example, I was reading an input of 20 lines and I wanted to strip the first 3 and the last 7. If I resolved to do so w/ sed , I would do it with a tail buffer. I would first add together three and seven for a total strip count of ten and then do:

That is an example which strips the first 3 and last 7 lines from input. The idea is that you can buffer as many lines as you wish to strip from the tail of input in the pattern space on a stack but only P rint the first of these for every line pulled in.

  • On lines 1,10 sed P rints nothing because for each of those it is stacking input in pattern space line-by-line in a b ranch loop.
  • On the 3rd line all of sed ‘s stack is d eleted — and so the first 3 lines are stripped from output in one fell swoop.
  • When sed reaches the $ last line of input and attempts to pull in the N ext it hits EOF and stops processing entirely. But at that time pattern space contains all of lines 14,20 — none of which have yet been P rinted, and never are.
  • On every other line sed P rints only up to the first occurring \n ewline in pattern space, and D eletes same before beginning a new cycle with what remains — or the next 6 lines of input. The 7th line is appended again to the stack with the N ext command in the new cycle.

And so, of seq ‘s output (which is 20 sequentially numbered lines), sed only prints:

This gets to be problematic when the number of lines you desire to strip from the tail of input is large — because sed ‘s performance is directly proportional to the size of its pattern space. Still, though, it is a viable solution in many cases — and POSIX specs a sed pattern space to handle at least 4kb before busting.

Источник

Оцените статью