I have a Bash script that writes data into a log file, then from that I take out stuff I want using the sed command. That gives me a file data.txt. What I want to know is how I go about deleting specific line inside that data.txt file.

For example:


I want to remove the 2nd line, containing 456 so that I only have



I tried with sed '2d' data.txt but it doesn't work.

I use this command to create data.txt from log.log:

sed -nE '1s/.{1}(.{2}).*/\1/p;' log.log >> data.txt

Try this. This should work.

sed -i '2d' data.txt

You have to add -i flag with sed command unless you are redirecting your output to a new file. If you don't add the -i flag, sed will print the pattern space to STDOUT and will not make any changes to the original file.

Automatic backup option

It is quite dangerous to modify a file without taking a proper backup. So sed has its native method to backup a file before editing which is -i.bak option.

So in the above example if we use the backup option, the command would be.

sed -i.bak '2d' data.txt

So it will remove the first line from the file data.txt and will also make a backup copy of the original file with .bak extension.

  • Your answer worked like a charm just what i was looking for, thanks :) – Insanebench420 Jan 4 '18 at 12:39
  • 1
    @pa4080 Added :) – ran Jan 4 '18 at 13:03
  • I've removed this part from my answer. – pa4080 Jan 4 '18 at 13:06

You know, besides specifying by line number, you can also do it by matching the content of the line, if that's more useful (which sounds like it might):

sed -i '/^456$/d' data.txt

Using awk:

awk 'NR!=2{ print }' input-file > output-file

That could be read as: if the number of record is not equal to 2 then print the record (line). If you want to delete the second and the third line:

awk 'NR!=2 && NR!=3 { print }' input-file

If you want to have an empty line as it is shown into the question, the sed command should look as this:

 sed '2 s/^.*$//' input-file

The first character 2 is the number of the line, followed by the command s, that will substitute all characters .* from the beginning ^ to the end $ of the line with empty string //. In this case the beginning and the end characters could be omitted.


$ cat input-file

$ sed '2,3 s/.*//' input-file


This command will delete lines 2-4 and 6

sed -e '2,4d;6d' file
  • 'sed: -e expression #1, char 3: missing command' i get this error, i use only sed -e '2,3' file – Insanebench420 Jan 4 '18 at 12:19
  • 1
    @Insanebench420, this works for me, also the example from your question sed '2d' works. What is your Ubuntu version? – pa4080 Jan 4 '18 at 12:24
  • 1
    you cannot use the command as you did (sed -e '2,3' ) you should have 'd' after the line numbers. If you want to specify a range 2-34 use '2,34d' If you want to just delete lines 2 and 34 use '2d,34d' – Petr Jan 4 '18 at 12:26
  • @Petr that should probably be 2d;34d (not a comma). – JoL Jan 4 '18 at 19:20
  • @JoL Sorry you were right :) I'm not able to edit it now. – Petr Jan 5 '18 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.