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
  • 90,984
  • 81
  • 374
  • 818
Fogarasi Norbert
  • 598
  • 1
  • 9
  • 29

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
  • 15,783
  • 5
  • 62
  • 95
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
  • 158,107
  • 27
  • 234
  • 292