批处理实现文本内容居中显示的代码

a.txt  
代码:

[Promise don't come easy]
I should have known all along.
There was something wrong.
I just never read between the lines.
Then I woke up one day and found you on your way.
Leaving nothing but my heart behind.
What can I do to make it up to you.
Promises don't come easy.
But tell me if there's a way to bring you back home to stay.
Well I'd promises anything to you.
I've been walkin' around with my head hanging down.
Wondrin' what I'm gonna do.
'Cause when you walked out that door.
要求,CMD原始窗口中,不调节窗口大小,不生成临时文件,居中显示文本内容。输出如下:
代码:

[Promise don't come easy]
I should have known all along.
There was something wrong.
I just never read between the lines.
Then I woke up one day and found you on your way.
Leaving nothing but my heart behind.
What can I do to make it up to you.
Promises don't come easy.
But tell me if there's a way to bring you back home to stay.
Well I'd promises anything to you.
I've been walkin' around with my head hanging down.
Wondrin' what I'm gonna do.
'Cause when you walked out that door.

Batcher:

sed -e :a -e "s/^.\{1,77\}$/ & /;ta" a.txt

sed.exe
http://xiazai.jb51.net/201010/tools/sed_jb51.rar 

batman:思路说明:
以下代码是充分findstr命令的示例,使用findstr /n来取得行号同时兼容空行,
并将行号进行变量赋值;使用findstr /o来取得第行的字符偏移量,但要通过
上、下行的字符偏移差,因为字符偏移值是类计递加的,同时每行回车增加
两个字符偏移,所以不可避免要对最后一行强加回车,在不破坏原文件的情
况下只好生成临时文件;利用findstr /o能够一次性获得文本所有行的字符偏
移量也就是字符总数,效率和逐字符判断相比肯定是高得多了。
---------------------------------


代码如下:

@echo off
copy /y 1.txt 2.txt>nul
echo.>>2.txt&echo end>>2.txt
for /f "tokens=1,2* delims=: " %%i in ('findstr /n /o .* 2.txt') do (
set "_%%i=%%j"&set ".%%i=%%k"
  if %%i geq 2 call :lp %%i
)
del /q 2.txt&pause>nul&goto :eof
:lp
set /a n=%1-1
set /a num=_%1-_%n%-2
set /a kl=(80-num)/2
for /l %%i in (1,1,%kl%) do set /p= <NUL
call,set /p=%%.%n%%%<NUL&ECHO.

(0)

相关推荐

  • 批处理实现文本内容居中显示的代码

    a.txt 代码: [Promise don't come easy] I should have known all along. There was something wrong. I just never read between the lines. Then I woke up one day and found you on your way. Leaving nothing but my heart behind. What can I do to make it up to y

  • shell脚本函数:控制颜色、定位、居中显示的代码

    一.控制颜色 代码: 复制代码 代码如下: #!/bin/sh function colour (){  case $1 in    black_green)       echo -e '\033[40;32m' black_yellow)       echo -e '\033[40;33m' black_white)       echo -e '\033[40;37m' black_cyan)       echo -e '\033[40;36m' black_red)       ec

  • 批处理实现文本内容横列变纵列的代码

    因为论坛会吞掉文本每行的第一个空格,现截图上来. 每行的字符数都一样,但未知多少个字符. 注意,文本第三行r之后有一个空格.第四行x之后有一个空格. 输出为: 即横列变纵列,并保留原来空格. 要求,高效,不生成临时文件. batman: 复制代码 代码如下: @echo off for /f "delims=" %%. in (1.txt) do set "_=0"&set "..=%%."&call :{} for /l %%+

  • asp.net 文章内容分页显示的代码

    aspx: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ArticlePage.aspx.cs" Inherits="ArticlePage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/

  • 交换两个文本内容的C语言代码

    文本存储的位置: jack.txt位于:    e:\jack.txt retchie.txt位于:     e:\retchie.txt 内容: jack.txt   ->  "Hello! I am Jack." retchie.txt   ->   "Hello! I am Retchie." 相关代码: 复制代码 代码如下: #include <stdio.h> int main(void){    char temp1[100]; 

  • 批处理实现一行内容分行输出的代码

    a.txt: aA1一bB2二cC3三dD4四eE5五fF6六gG7七hH8八iI9九 输出如下: abcdefghi ABCDEFGHI 123456789 一二三四五六七八九 唯一要求,老手略过,留给新手做练习,不能直接echo. 请先发现其中的规律. 其他,生成临时文件.代码超过一页都不重要,正确输出即可. 523066680: 复制代码 代码如下: @echo off setlocal enabledelayedexpansion set a=-1 for /f "tokens=*&qu

  • 在textarea文本域中显示HTML代码的方法

    这是一个iframe 的 给你一个参考的: 复制代码 代码如下: <iframe ID="tryit" MARGINHEIGHT="1" MARGINWIDTH="1" width="100%" height="300" scrolling="auto"></iframe>      <script language=javascript>     

  • Android 让自定义TextView的drawableLeft与文本一起居中

    前言 TextView的drawableLeft.drawableRight和drawableTop是一个常用.好用的属性,可以在文本的上下左右放置一个图片,而不使用更加复杂布局就能达到,我也常常喜欢用RadioButton的这几个属性实现很多效果,但是苦于不支持让drawbleLeft与文本一起居中,设置gravity为center也无济于事,终于有空研究了一下,这里与大家一起分享. 正文 一.效果图  二.实现代码 自定义控件 /** * drawableLeft与文本一起居中显示 * *

  • PyQt5实现tableWidget 居中显示

    目录 PyQt5 tableWidget 居中显示 PyQt5 TableWidGet问题 PyQt5 tableWidget 居中显示 newItem = QTableWidgetItem("内容") # 居中显示 newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter) PyQt5 TableWidGet问题 使用pyqt5展示excel的数据到桌面,并获取选中的数据内容 from PyQt5 import QtCore

  • asp文本框换行显示代码

    复制代码 代码如下: <% function CHTMLEncode(fString) fString = Replace(fString, CHR(13), "") fString = Replace(fString, CHR(10), "<BR> ") CHTMLEncode= fString end function %> 调用时候使用 <%=CHTMLEncode(rs("字段内容"))%> PHP文本

随机推荐