0

I have a text file that looks like this:

    FD488AFF,
    FED6ECFF,
    FFA2A2FF,
    FFD7D8FF,
(
)

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.

2
  • @scott you are ranting. – ctrl-alt-delor Nov 20 '18 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 Nov 20 '18 at 20:09
3

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'
$-1,$d
$s/,$//
wq
END
1
  • Original solution! +1 – Fabby Nov 20 '18 at 20:59
2

Not the most effective, but should do the job:

sed '$d' file | sed '$d' | sed '$s/,$//'
    FD488AFF,
    FED6ECFF,
    FFA2A2FF,
    FFD7D8FF
1
  • s/effective/efficient/ -- this solution is perfectly effective. – glenn jackman Nov 20 '18 at 23:07
0

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;${
s/,\n(\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

1
  • 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 – don_crissti Nov 20 '18 at 20:48
0

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.

1
  • The requirement is not "delete lines beginning with ( or )", it is "delete last 2 lines" – glenn jackman Nov 20 '18 at 23:07
-1

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/,$//'

or

< «file-name»  sed -nr -e 'x;${d;x;d};1d;p' | sed -r -e '$ s/,$//'
1
  • (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. – ctrl-alt-delor Nov 21 '18 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.