0

I have a fiel with an awkward file end.

cat splitted.0473.csv | tail -c 8 | od -c
0000000   6   4   0  \r  \n 032  \r  \n
0000010    

The 032 is a ^Z (know as substitution character).

I want to remove the last 3 characters (bytes?) from the end of the file so it look like this:

cat splitted.0473.csv | tail -c 8 | od -c
0000000   6   4   0  \r  \n
0000010    

I tried some sed examples but didn't succeed. I assume that removing the last n characters is easier to realize with sed (or anything else) then replace specific characters ^Z\r\n from the file end.

2
  • 1
    truncate -s -3 file.csv with GNU truncate would shorten it by 3 bytes. Oct 26, 2022 at 8:11
  • 1
    Old Windows editors used to use Ctrl-Z to signal end-of-file (like Ctrl-D in Linux), but they explicitly stored the character in the data file (huh?). You might consider using dos2unix to remove the CR (\r) -- they are a frequent source of issues. Oct 26, 2022 at 10:06

1 Answer 1

2

You should be able to delete the last line with sed:

sed -i '$d' splitted.0473.csv

-->

$ printf '640\r\n\032\r\n' | sed '$d' | od -c
0000000   6   4   0  \r  \n
0000005

You must log in to answer this question.

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