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.

don_crissti
  • 79,330
  • 30
  • 216
  • 245
mcbeav
  • 79
  • 8
  • @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

5 Answers5

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
glenn jackman
  • 84,176
  • 15
  • 116
  • 168
2

Not the most effective, but should do the job:

sed '$d' file | sed '$d' | sed '$s/,$//'
    FD488AFF,
    FED6ECFF,
    FFA2A2FF,
    FFD7D8FF
RudiC
  • 8,889
  • 2
  • 10
  • 22
1

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

don_crissti
  • 79,330
  • 30
  • 216
  • 245
  • 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.

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
Lewis M
  • 1,038
  • 4
  • 5
-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/,$//'
ctrl-alt-delor
  • 27,473
  • 9
  • 58
  • 102
  • (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 `head`s 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