批处理

批处理

实验要求

有两个文本如下,实际中并不知道两文本各有多少行:

文本1.txt  
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 
ccccccccccccccccccccccccccccccccccccccc 
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 
ggggggggggggggggggggggggggggggggggggggg 
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww 
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 

文本2.txt 
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh 
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii 
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj 
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn 

要求用Windows下批处理和Linux下的shell脚本完成,两文本交替输出:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh 
ccccccccccccccccccccccccccccccccccccccc 
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii 
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj 
ggggggggggggggggggggggggggggggggggggggg 
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn 
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww 
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 

要求:
1 尽量不生成临时文件
2 代码要高效,代码尽量简洁
3 给出各种情况的测试结果截图(正常文件,巨大文件,一个空文件,两个空文件)

Windows

@echo off
 
set "paths=1.txt"
for %%a in ("%paths%") do (
 if "%%~za" equ "0" (
   echo "1.txt empty"
   
    ) else (
    echo "1.txt not empty"
 )
)
 
set "paths=2.txt"
for %%a in ("%paths%") do (
 if "%%~za" equ "0" (
   echo "2.txt empty"
   pause
    ) else (
    echo "2.txt not empty"
 )
)
 
 
for /f "delims=" %%i in (1.txt) do set /a n+=1&call,set "_%%n%%=%%i"
for /f "delims=" %%i in (2.txt) do set /a m+=1&call,set ".%%m%%=%%i"
if %n% gtr %m% (set "num=%n%") else (set "num=%m%")
for /l %%i in (1,1,%num%) do (
if defined _%%i call,echo %%_%%i%%
if defined .%%i call,echo %%.%%i%%
)
 
pause>nul

正常文件,正确输出
截图
巨大文件,终端卡死
截图
空文件,识别为空
截图

Linux

#!/bin/bash

file1="文本1.txt"
file2="文本2.txt"

# 使用 paste 命令交替输出
paste -d '\n' $file1 $file2

正常文件,正确输出
截图
巨大文件,持续输出直到完成后停止
截图
空文件,无输出
截图

posted @ 2025-01-02 11:33  Arisf  阅读(3)  评论(0编辑  收藏  举报