1.windows:
代码如下test.bat
@echo off
set "n=-1"&set "flag=a"
:begin
set /a n+=1
if %n% equ 0 (set "m=") else (set "m=skip=%n%")
for /f "%m% delims=" %%i in (1.txt) do echo %%i&if defined flag goto next
set no=a
:next
for /f "%m% delims=" %%i in (2.txt) do echo %%i&goto begin
if not defined no set "flag="&goto begin
pause>nul
@echo off&setlocal enabledelayedexpansion
set "num=0"
for %%a in (1.txt 2.txt) do (
for /f "delims=" %%i in (%%a) do (
set /a n+=1
if !n! gtr !num! set "num=!n!"
set /p=%%i <nul>>temp.txt
)
echo.>>temp.txt&set "n=0"
)
:lp
set /a n+=1
for /f "tokens=%n%" %%i in (temp.txt) do echo %%i
if %n% neq %num% goto lp
del /q temp.txt&pause>nul
正常:
大文件:
2空:
都空:
2.Linux:
test.sh代码如下:
#! /bin/bash
function printLine() {
pri=`cat $1 | head -n$2 | tail -1f`
#字符串长度是否为0,不为0输出
if [ -n "${pri}" ]
then
echo ${pri}
fi
}
file1=./1.txt
file2=./2.txt
m=`cat ./${file1} | wc -l`
n=`cat ./${file2} | wc -l`
max=
min=
moreTxt=
if [ ${m} -gt ${n} ]
then
max=${m}
min=${n}
moreTxt="${file1}"
else
max=${m}
min=${n}
moreTxt="${file2}"
fi
i=1
while (( i <= ${min} ))
do
printLine ${file1} ${i}
printLine ${file2} ${i}
let i++
done
while (( i <= ${max} ))
do
printLine ${moreTxt} ${i}
let i++
done
正常:
大文件:
1空:
都空: