简体   繁体   English

在Windows上sed,删除除指定字符外的所有字符

[英]sed on windows, remove all characters except specified

sed -c -i -r 's/[^a-zA-Z 0-9`~!@#$%^&*()_+\[\]\\{}|;'\'':",.\/<>?]//g' report.csv

I'm using sed for windows from http://gnuwin32.sourceforge.net/packages/sed.htm 我正在从http://gnuwin32.sourceforge.net/packages/sed.htm将sed用于Windows

Can't seem to get things escaped properly, I've got csv files with lots of junk characters like NUL, BEL, and several others that I need to strip from files but I don't know which ones may be present so trying to do the inverse and only permit ones I want. 似乎无法正常逃避,我的csv文件包含很多垃圾字符,例如NUL,BEL和其他一些我需要从文件中删除的字符,但我不知道可能存在哪些文件,因此尝试反之,只允许我想要的。 Needs to be called from a script on the windows command line. 需要从Windows命令行上的脚本中调用。 sed is a tool i'm reasonably familiar with and for the most part it works, but I can't seem to get this one working. sed是我相当熟悉的一种工具,并且在大多数情况下都可以使用,但是我似乎无法使该工具正常工作。 With the above it says The system cannot find the path specified, if I change the quotes and escape characters i get other errors so don't get hung up on any particular one. 有了上面的内容,它说系统找不到指定的路径,如果我更改引号和转义字符,则会遇到其他错误,所以不要挂在任何特定的路径上。 The file referenced is in the same folder the command is executed on. 引用的文件在执行命令的文件夹中。

You didn't escape everything properly: 您没有正确地逃避一切:

's/[^a-zA-Z 0-9`~!@#$%^&*()_+\[\]\\{}|;'\'':",.\/<>?]//g'
^--start string           end string---^

which makes the \\ after that "end string" quote a path separator for cmd.exe, not part of your sed command. 这使得“结束字符串”后的\\引用cmd.exe的路径分隔符,而不是sed命令的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM