29

How do you delete all lines in a file that begin with "string" in sh? I was thinking about using the sed command.

t3hcakeman
  • 2,129
  • 4
  • 24
  • 25

4 Answers4

36
grep -v '^string' yourfile.txt > stripped.txt
Marc B
  • 348,685
  • 41
  • 398
  • 480
33

To do it in place, if your sed supports the -i option, you can do:

sed -i '/^string/d' input-file
William Pursell
  • 190,037
  • 45
  • 260
  • 285
3

sed and grep in your answers are missing their friend awk:

awk '!/^string/' inputfile > resultfile
Kent
  • 181,427
  • 30
  • 222
  • 283
0

You can use Vim in Ex mode:

ex -sc g/^string/d -cx file
  1. g select all matching lines

  2. d delete

  3. x save and close

Zombo
  • 1
  • 55
  • 342
  • 375