0

I'm using Ubuntu. I have an fileName.sh file containing 1000 lines and I have to delete the 399 number line is this command is sufficient for the deleting the line.

Command:- sed -i '399d' test.sh

Is there any need to change in this command? And can you please give me more suggestions to delete the line from a file?

9
  • 3
    Have you tried running the command and verifying the result manually ? What was the result ?
    – Soren A
    Jan 14, 2019 at 7:21
  • yes I tried this command in result it will delete the line from the file @SorenA
    – catter
    Jan 14, 2019 at 7:23
  • So why the question ?
    – Soren A
    Jan 14, 2019 at 7:27
  • 1
    IMHO deleting by line number is dangerous, if there is any change in what produces the target file you'll be deleting the wrong line. Deleting by pattern woul be a bit safer (though still dangerous if done on a script).
    – xenoid
    Jan 14, 2019 at 7:34
  • @xenoid whatever method you do it all has the same risk. If 1 of the actual arguments is "is has to be line 399" I do not see anything wrong. I would assume someone would 1st verify it is indeed line 399.
    – Rinzwind
    Jan 14, 2019 at 7:37

1 Answer 1

0

Better (but stiull unsafe) to remove the line using its contents than using a line number. If you want to delete a line whose contents are known:

sed -i /{contents}/d in.sh

Another solution is to use grep -v:

grep -v '{contents}' <in.sh >out.sh

In both cases {contents} is a regexp that matches the contents you want to remove, knowing that strings with only letters and numbers and a few select special characters are regular expressions for themselves, so:

sed -i /somefile/d in.sh
grep -v 'somefile' <in.sh >out.sh

will remove any line containing somefile from the input. But keep in mind that they could also match more things (for instance somefile2 would also be matched) unless you start using more complete regexps, for instance using ^{contents}$ to match lines that are exactly {contents}, excluding those that contain {contents} and additional things.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .