6人参与 • 2025-04-24 • Linux
linux
中的换行符对于格式化文本输出、修改文件和确保跨系统兼容性至关重要。
linux
主要使用 lf
(换行符,\n
)来换行,而 windows
使用 crlf
(回车符 + 换行符,\r\n
)
cat -a myfile.txt
输出 linux
风格,lf \n
:
hello world$
输出 windows
风格,crlf \r\n
:
hello world^m$
$
:表示行的结束 (lf
)
^m$
:表示文件有 windows
换行符 (\r\n
)
od -c myfile.txt
输出:linux \n
0000000 h e l l o w o r l d \n
输出:windows \r\n
0000000 h e l l o w o r l d \r \n
\r \n
: windows
样式的行尾
\n
: linux
风格的行尾
dos2unix
dos2unix myfile.txt
sed
sed -i 's/\r$//' myfile.txt
tr
cat myfile.txt | tr -d '\r' > newfile_unix.txt
unix2dos
unix2dos myfile.txt
sed
sed -i 's/$/\r/' myfile.txt
awk
awk '{print $0 "\r"}' myfile.txt > newfile_windows.txt
echo -e
echo -e "line 1\nline 2\nline 3"
输出:
line 1 line 2 line 3
printf
printf "line 1\nline 2\n"
echo "hello world" | sed 's/ / \n/g'
输出:
hello world
awk
echo "hello world" | awk '{print $1 "\n" $2}'
#!/bin/bash while ifs= read -r line; do echo "processing: $line" done < myfile.txt
sed -i '/^$/d' myfile.txt 或 awk 'nf' myfile.txt > clean_file.txt
grep -c '^' myfile.txt 或 wc -l myfile.txt
echo "new entry" >> myfile.txt
echo -n "new entry" >> myfile.txt
tail -c1 myfile.txt | od -c
如果输出显示 \n
,则表示文件尾部有换行符。
如果没有输出,则文件缺少尾随换行符。
mytext="line 1 line 2 line 3" echo "$mytext"
cat <<eof > myfile.txt this is line 1. this is line 2. eof
sed
sed -i 's/\r$//' myfile.txt
vim
vim myfile.txt :set fileformat=unix :wq
以上就是linux换行符的使用方法详解的详细内容,更多关于linux换行符使用的资料请关注代码网其它相关文章!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论