3

I am just learning to work with csv files from the command line. I want to delete several lines from a file using sed. I've removed the header of a file with this cat file.csv | sed 1,2d > file.csv.

Now I want to delete several more lines from the file (lines 3, 10, 12, and 28-35) and I am not sure how to pull it off. I'd be grateful for any help.

2 Answers 2

4

Depending on the sed implementation, you could separate them as follows:

cat file.csv | sed "1,2d;10d;12d;28,35d" > file2.csv
Sign up to request clarification or add additional context in comments.

5 Comments

I'm getting errors: -bash: 10d: command not found and the same for 12d and 28,35d. As with before, 1,2d is working. Is it possible that the structure of the csv is to blame?
The file would not be the cause. My example must not be valid in all cases. I was running it on Windows with what may likely be a version of sed not fully compatible. I'll have to test it more tomorrow. Sorry for the bad lead.
Some seds don't implement the semi-colon. Use the -e flag several times.
@Jeff: It probably is not an improvement over just using multiple commands via -e, but quoting the script appears to make it work.
If you get an error from bash it's probably because you forgot to use the quotation marks - it thinks ";" starts a new command and tries to run the 10d command.
2

Use the -e flag to pass several commands to one sed invocation, like this:

seq 1 40 | sed -e 1,2d -e 3d -e 10d -e 12d -e 28,35d

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.