8

How can remove the last comma separator from a file on Linux?

Example of file :

"is_supported_kafka_ranger" : "true",
"kafka_log_dir" : "/var/log/kafka",
"kafka_pid_dir" : "/var/run/kafka",
"kafka_user" : "kafka",
"kafka_user_nofile_limit" : "128000",
"kafka_user_nproc_limit" : "65536",

expected results:

"is_supported_kafka_ranger" : "true",
"kafka_log_dir" : "/var/log/kafka",
"kafka_pid_dir" : "/var/run/kafka",
"kafka_user" : "kafka",
"kafka_user_nofile_limit" : "128000",
"kafka_user_nproc_limit" : "65536"
22

Using GNU sed:

sed -i '$s/,$//' file

That is, on the last line ($) substitute (s) the comma at the end of the line (,$) by nothing.

The change will be done in-place due to the -i flag.

With standard sed:

sed '$s/,$//' <file >file.new &&
mv file.new file

Note: Someone suggested an edit to change "on the last line" to "last on the line" (or something similar). This is wrong. When $ is used to specify an address (a line where to apply an editing command), then it refers to the last line of the stream or file. This is different from using $ in a regular expression.

6

For in-place editing you could use ed - conveniently, it sets the position to the last line by default on opening so you don't need to address the last line explicitly:

ed file
s/,$//
wq

Or, as a one-liner

printf 's/,$//\nwq\n' | ed -s file
  • 2
    In bash you can dispense with printf like this: ed -s file <<< $'s/,$//\nwq\n' (the keyword for search in the bash manual is "here string"). – Ruslan Jun 3 '18 at 18:35
5

In general, I'd probably go with the straightforward sed solution. However, if your input files are huge, you might want a solution that doesn't take the time to read through the whole file just to edit the last couple of bytes.

If you are 100% sure your input file ends with a comma followed by a newline, you can use truncate to lop off these last two characters, and then re-append a final newline:

filesize=$(stat -L --format "%s" lastcomma.txt)
truncate --size $((filesize - 2)) lastcomma.txt 
echo >> lastcomma.txt 

This assumes a distro that includes GNU truncate or equivalent.


@StéphaneChazelas points out that GNU truncate now supports truncate -s -2 file to shorten the file by two bytes; if you have this version, the above simplifies to:

truncate --size -2 lastcomma.txt
echo >> lastcomma.txt 
  • ...and a filesystem that supports truncate – malat Jun 4 '18 at 8:43
  • @malat, what file system doesn't support truncate? – Stéphane Chazelas Jun 4 '18 at 8:59
  • @StéphaneChazelas Right this is poorly worded. How about: [...] and a filesystem that supports sparse file to have an efficient truncate. The OP describe his solution starting with: " if your input files are huge"... hence my comment. – malat Jun 4 '18 at 9:02
  • 2
    GNU truncate now supports truncate -s -2 file to shorten the file by two bytes. You'd want to use stat -L as for symlinks, you want the size of the target of the symlink (or simply size=$(wc -c < file)). – Stéphane Chazelas Jun 4 '18 at 9:24
  • 2
    @malat, you don't need sparse file support to truncate files. Here, we're trimming data off the end of the file. – Stéphane Chazelas Jun 4 '18 at 9:26

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.