利用shell命令去除dos文件里的回车符——“^M”

讨论其他BSD项目及其衍生品,如:FreeBSD,DragonFlyBSD,PC-BSD,DesktopBSD,FreeNAS,NAS4Free,m0n0wall,pfSense,pacBSD,OpenDarwin...
回复
头像
leo
帖子: 2465
注册时间: 2010-01-21 3:27

利用shell命令去除dos文件里的回车符——“^M”

帖子 leo » 2011-03-16 12:34

《shell脚本学习指南》学习笔记:测试环境 OpenBSD 4.8 i386 stable.
这里假设原来的dos系统文件为dos.txt,修正后的unix系统文件名为unix.txt,利用标准的输入输出命令:

代码: 全选

# tr -d '\r' < dos.txt > unix.txt

在OpenBSD下进行一下文件对比,源文件dos.txt

代码: 全选

<if condition="$show['lastpostinfo']">[color=red]^M[/color]
<div class="smallfont" align="$stylevar[align=left]">[color=red]^M[/color]
    <div>[color=red]^M[/color]
        <span style="white-space:nowrap">[color=red]^M[/color]
        <if condition="$show['icon']"><img class="inlineimg" src="$icon[iconpath]" alt="$icon[title]" border="0" /></if>[color=red]^M[/color]
        <if condition="$lastpostinfo['prefix']">[color=red]^M[/color]
            $lastpostinfo[prefix][color=red]^M[/color]
            </span><if condition="is_browser('safari')"><!--bug#24775--> </if>[color=red]^M[/color]
            <span style="white-space:nowrap">[color=red]^M[/color]
        </if>[color=red]^M[/color]
        <a href="showthread.php?$session[sessionurl]goto=newpost&t=$lastpostinfo[lastthreadid]" style="white-space:nowrap" title="<phrase 1="$lastpostinfo[lastthread]">$vbphrase[go_first_unread_in_thread_x]</phrase>"><strong>$lastpo$
    </div>[color=red]^M[/color]
    <div style="white-space:nowrap<if condition="is_browser('ie', 6)">; float:$stylevar[align=left]</if>">[color=red]^M[/color]
        <phrase 1="member.php?$session[sessionurl]find=lastposter&f=$lastpostinfo[forumid]" 2="$lastpostinfo[lastposter]">$vbphrase[by_x]</phrase>[color=red]^M[/color]
    </div>[color=red]^M[/color]
    <div align="$stylevar[align=right]" style="white-space:nowrap">[color=red]^M[/color]
        $lastpostinfo[lastpostdate] <if condition="!$show['detailedtime']"><span class="time">$lastpostinfo[lastposttime]</span></if>[color=red]^M[/color]
        <a href="showthread.php?$session[sessionurl]p=$lastpostinfo[lastpostid]#post$lastpostinfo[lastpostid]"><img class="inlineimg" src="$stylevar[imgdir_button]/lastpost.gif" alt="$vbphrase[go_to_last_post]" border="0" /></a>[color=red]^M[/color]
    </div>[color=red]^M[/color]
</div>[color=red]^M[/color]
<else />[color=red]^M[/color]
<div class="smallfont" align="$stylevar[align=right]">[color=red]^M[/color]
    $lastpostinfo[lastpostdate] <if condition="!$show['detailedtime']"><span class="time">$lastpostinfo[lastposttime]</span></if>[color=red]^M[/color]
    <a href="showthread.php?$session[sessionurl]p=$lastpostinfo[lastpostid]"><img class="inlineimg" src="$stylevar[imgdir_button]/lastpost.gif" alt="$vbphrase[go_to_last_post]" border="0" /></a>[color=red]^M[/color]
</div>[color=red]^M[/color]
</if>

Unix.txt

代码: 全选

<if condition="$show['lastpostinfo']">
<div class="smallfont" align="$stylevar[align=left]">
    <div>
        <span style="white-space:nowrap">
        <if condition="$show['icon']"><img class="inlineimg" src="$icon[iconpath]" alt="$icon[title]" border="0" /></if>
        <if condition="$lastpostinfo['prefix']">
            $lastpostinfo[prefix]
            </span><if condition="is_browser('safari')"><!--bug#24775--> </if>
            <span style="white-space:nowrap">
        </if>
        <a href="showthread.php?$session[sessionurl]goto=newpost&t=$lastpostinfo[lastthreadid]" style="white-space:nowrap" title="<phrase 1="$lastpostinfo[lastthread]">$vbphrase[go_first_unread_in_thread_x]</phrase>"><strong>$lastpo$
    </div>
    <div style="white-space:nowrap<if condition="is_browser('ie', 6)">; float:$stylevar[align=left]</if>">
        <phrase 1="member.php?$session[sessionurl]find=lastposter&f=$lastpostinfo[forumid]" 2="$lastpostinfo[lastposter]">$vbphrase[by_x]</phrase>
    </div>
    <div align="$stylevar[align=right]" style="white-space:nowrap">
        $lastpostinfo[lastpostdate] <if condition="!$show['detailedtime']"><span class="time">$lastpostinfo[lastposttime]</span></if>
        <a href="showthread.php?$session[sessionurl]p=$lastpostinfo[lastpostid]#post$lastpostinfo[lastpostid]"><img class="inlineimg" src="$stylevar[imgdir_button]/lastpost.gif" alt="$vbphrase[go_to_last_post]" border="0" /></a>
    </div>
</div>
<else />
<div class="smallfont" align="$stylevar[align=right]">
    $lastpostinfo[lastpostdate] <if condition="!$show['detailedtime']"><span class="time">$lastpostinfo[lastposttime]</span></if>
    <a href="showthread.php?$session[sessionurl]p=$lastpostinfo[lastpostid]"><img class="inlineimg" src="$stylevar[imgdir_button]/lastpost.gif" alt="$vbphrase[go_to_last_post]" border="0" /></a>
</div>
</if>

源文件的基本内容并没有变化,尺寸小了,在OpenBSD下看得也舒服多了。

代码: 全选

# ls -la
....
-rw-r--r--   1 root  wheel     1899 Mar 15 23:23 dos.txt
-rw-r--r--   1 root  wheel     1874 Mar 15 23:22 unix.txt

chenjun
铁 Fe
帖子: 56
注册时间: 2010-08-31 17:38

帖子 chenjun » 2011-03-16 14:29

这个给力,确实很有用:D

回复

在线用户

正浏览此版面之用户: 没有注册用户 和 33 访客