[英]replace chracter with new line using sed command in PHP file
我正在處理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 \"sed -i -r 's/< /\\n/' $text_files_path/diffFile.txt 2>&1\"");
我在bash命令周圍使用(轉義)雙引號,在sed命令周圍使用單引號。 我還將\\n
更改為\\\\n
,因為反斜杠需要在雙引號中轉義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.