I have a text file that looks like this:


This also has a newline character after the last ) character. I am using the macOS terminal and looking for a way to remove the last comma, the (, and the last ), and the return characters following them.


and the new line character following the last ) character, but I can't seem to get this to work. I don't have to use sed, but it does need to be using the terminal.

  • @scott you are ranting. Commented Nov 20, 2018 at 19:58
  • @Scott I use a colorpicker app called Colorsnapper2. It does everything I need it to do besides sync between computers. I use 3 different computers for web development depending on if I'm on the road, or at home, or at work. The short of it is I'm writing a few scripts to backup and restore the current colors I'm using in the app, and using a cloud service to sync them between computers. I don't need to explain in detail why I need to do it, as it doesn't matter to you. I asked a fairly simple question. I didn't ask for advice on if I should do this or why I shouldn't. Thanks for the help.
    – mcbeav
    Commented Nov 20, 2018 at 20:09

5 Answers 5


Reverse the file, remove the first 2 lines and remove the comma on the 3rd line. Then re-reverse the results.

tac file | sed '1,2d; 3s/,$//' | tac

To save back into the same file:

... | tac > tmp && mv tmp file

or, using sponge from the moreutils package:

... | tac | sponge file

On second thought, this will be a great way to do it: ed is actually a file editor so you can make your edits and save.

ed file <<'END'
  • Original solution! +1
    – Fabby
    Commented Nov 20, 2018 at 20:59

Not the most effective, but should do the job:

sed '$d' file | sed '$d' | sed '$s/,$//'
  • s/effective/efficient/ -- this solution is perfectly effective. Commented Nov 20, 2018 at 23:07

To remove the last two lines if they contain a left and respectively a right paren and the comma that precedes them you could use a sliding window:

sed  '1N;$!N;${
P;D' infile

This pulls in a New line when on 1st line and then it always keeps three lines in the pattern space (via a N;P;D cycle). When it gets to the la$t line, it attempts1 to remove a comma followed by a \newline, a (, another \newline and a )

1: no need to anchor here since there can't be more than three lines in the pattern space at any time

  • This doesn't edit the file in-place. Consult the manual to see if your sed supports editing in place and what is the syntax. If not supported use a temporary file e.g. sed 'code' infile > outfile && mv outfile infile Commented Nov 20, 2018 at 20:48

I'm not sure of a way of doing it with one sed command since you want to remove the last , but it is not the last line. Here is a way I found to do it using a pipe:

sed -e '/^[()]/d' _file_ | sed -e '$s/,$//'

The first sed removes the lines that start with ( or ). The second sed removes the comma at the end of a line from the last line only.

  • The requirement is not "delete lines beginning with ( or )", it is "delete last 2 lines" Commented Nov 20, 2018 at 23:07

To remove last two lines by content

see @LewisM's answer https://unix.stackexchange.com/a/483043/4778

To remove last two lines by position, and the comma from end of 3rd from end.

< «file-name»  head -n -2 | sed -r -e '$ s/,$//'


< «file-name»  sed -nr -e 'x;${d;x;d};1d;p' | sed -r -e '$ s/,$//'
  • (I have a down vote) Does it not answer the question? Dose it not work? I did test it, and it does work on Debian. Apparently not all heads can except negative numbers. That is why I have given a pure sed implementation of it. Commented Nov 21, 2018 at 16:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .