How can I delete words from each line of a file us

2020-05-02 02:32发布

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.

标签: linux bash shell
2条回答
The star\"
2楼-- · 2020-05-02 03:07

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+.

查看更多
SAY GOODBYE
3楼-- · 2020-05-02 03:25

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.

查看更多
登录 后发表回答