user3698832
user3698832

Reputation: 3

Sed command returns expression error

When I executed the sed command below, it returned the expression error as follows.

user% sed -e '$-3,$d' sample.csv
sed: -e expression #1, char 2: unknown command: `-'

What I wanted to do is to delete last 4 lines from the specified file. And I found some web-sites introducing the method above, but it did not work on my environment of Mac OSX 10.9 and CentOS. How can I fix this problem?

Upvotes: 0

Views: 79

Answers (2)

potong
potong

Reputation: 58391

This might work for you (GNU sed):

sed '1{N;N};N;$d;P;D' file

or the more adaptable:

sed ':a;$d;N;s/\n/&/4;Ta;P;D' file

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174706

If you want to delete last four lines in your file then try the below command,

tac file.csv | sed '1,4d' | tac > newfile.csv

Upvotes: 2

Related Questions