0

I transferred millions of generated SVG files from DOS to a Linux box and just realized that there is a ^@ as the last character of each file (the DOS end of file character) which is giving an error when I try to display the SVG file in a browser.

In this question: How can I remove the last character of the last line of a file?

Maroun gives the solution as:

sed '$ s/.$//' your_file

But when I modify it to look like this:

sed '$ s/.$//' *.SVG

or

find . -print | grep .SVG | sed '$ s/.$//'

It does not work.

I would also like to be able to specify that it should only delete the last character if it is the ^@.

Can someone please tell me what I am doing wrong or how to get this to work. The SVG files are in thousands of sub directories so I need to be able to make the change from top to bottom of the tree structure.

Community
  • 1
  • 1
Andrew
  • 33
  • 5
  • See: [How can I run dos2unix on an entire directory?](http://stackoverflow.com/q/11929461/3776858) – Cyrus Nov 22 '16 at 17:07

1 Answers1

1

I'm guessing your first example isn't working because it's taking only the last file expanded by *.SVG. The second example (the one with the find command) fails because you're passing find's output to sed instead of the file contents.

Also, if you want to change the files' contents with sed, you need to use -i so the changes are performed "in place".

You could try this:

for file in *.SVG
do
    sed -i '$ s/.$//' $file
done

Or:

find . -name "*.SVG" -exec sed -i '$ s/.$//' {} \;
Alvaro Carvajal
  • 966
  • 5
  • 6
  • Alvaro how do I make your command work only if the last character is ^@ ? – Andrew Nov 22 '16 at 19:13
  • type up to the period before the second $, after the period type ctrl-v ctrl-m (no space inbetween) then continue typing with the $... this should match the dos line-end as last character of the line – Stefan Hegny Nov 22 '16 at 20:33
  • So it would be: find . -name "*.SVG" -exec sed -i '$ s/.^v^m$//' {} \; I tried this in bash and it just put a ^M in the spot (.^M$). But the DOS end of file character is the ^@. How do I get the ^@? I tried ^v^@ but nothing happened. – Andrew Nov 24 '16 at 14:16