【发布时间】:2016-04-04 15:34:35
【问题描述】:
我正在处理 PHP 文件。我处理一些文本文件,并希望用新行替换每次出现的“
我正在使用 sed 命令:
shell_exec("C:\\cygwin64\\bin\\bash.exe --login -c 'sed -i -r \'s/'< '/'\n'/\' $text_files_path/diffFile.txt 2>&1'");
但是,它不起作用。
我正在使用:
shell_exec("C:\\cygwin64\\bin\\bash.exe --login -c 'sed -i -r \'s/.{2}//\' $text_files_path/diffFile.txt 2>&1'");
删除每行的前两个字符。
还有:
shell_exec("C:\\cygwin64\\bin\\bash.exe --login -c 'sed -i -r \'s/.{2}/*/\' $text_files_path/diffFile.txt 2>&1'");
用*替换第一个两个字符
这两个字符是'
解决方案:
我解决了这个问题:
shell_exec("C:\\cygwin64\\bin\\bash.exe --login -c \"sed -i -r 's/< /\\r\\n/' $text_files_path/diffFile.txt 2>&1\"");
对于新行,我写的是:\\r\\n 而不是 \\n。
非常感谢@Tom Fenech。
【问题讨论】:
-
试试
shell_exec("C:\\cygwin64\\bin\\bash.exe --login -c "tr '< ' '\n' < $text_files_path/diffFile.txt 2>&1""); -
@heemayl 不工作。我更喜欢使用 sed。
-
请注意,@heemayl 在