我试图使用命令行读取文件,从该文件中剪切一定数量的字符,然后将其写回文件。但是,我不需要任何换行符。
我看到的是,如果我使用echo -n "Example Text" > tmp_file
,我可以在没有任何换行符的情况下将数据写入文件。但是,当您使用cat读取文件时,您将获得换行文件。
如何读取文件,获取我需要的字符串并将其写回文件,而不被换行符所困扰。
任何帮助都将不胜感激。
可以使用tr
:
echo -n "Example Text" | tr -d "n" > tmp_file
或sed
:
echo -n "Example Text" | sed -z "s/n//g" > tmp_file
如果您只想删除输入末尾的换行符,可以这样做:
echo -e "Example TextnSome more Textnn" | sed -ze "s/[n]*$//g" > tmp_file
输出将是
Example Text
Some more Text (without newline here)