0

For e.g. I want to delete the second and the fourth word of each line

Before sample.txt:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Nulla elit dui, fermentum sed quam sed, semper auctor elit.

After sample.txt:

Lorem dolor amet, consectetur adipiscing elit.
Nulla dui, sed quam sed, semper auctor elit.
jww
  • 97,681
  • 90
  • 411
  • 885
Fogarasi Norbert
  • 650
  • 2
  • 14
  • 34

2 Answers2

1

Something like:

sed 's/\(\S\+\s\+\)\S\+\s\+\(\S\+\s\+\)\S\+\s\+\(.*\)/\1\2\3/g'

should work, if you must use sed. This script does not work if you have fewer than 4 words in the line.

OmnipotentEntity
  • 16,531
  • 6
  • 62
  • 96
0

You can do this with "cut"

cut -d ' ' -f1,3,5- MY_FILE

The -d sets the delimiter to space and the -f chooses fields 1, 3 and 5+.

tripleee
  • 175,061
  • 34
  • 275
  • 318