是否可以在Windows命令行中仅读取.sql文件的第一行?
如果是这样,我该怎么做 - 谷歌还没有提出有效的解决方案。
我所拥有的只是标准的窗口命令提示符
这看起来像是这个问题的副本;用于从文本文件中读取第一行的 Windows 批处理命令
引用该票证中接受的答案;
这是一个通用的批处理文件,用于打印像 GNU head
实用程序这样的文件中的前 n
行,而不仅仅是一行。
@echo off
if [%1] == [] goto usage
if [%2] == [] goto usage
call :print_head %1 %2
goto :eof
REM
REM print_head
REM Prints the first non-blank %1 lines in the file %2.
REM
:print_head
setlocal EnableDelayedExpansion
set /a counter=0
for /f ^"usebackq^ eol^=^
^ delims^=^" %%a in (%2) do (
if "!counter!"=="%1" goto :eof
echo %%a
set /a counter+=1
)
goto :eof
:usage
echo Usage: head.bat COUNT FILENAME
例如:
Z:>head 1 "test file.c"
; this is line 1
Z:>head 3 "test file.c"
; this is line 1
this is line 2
line 3 right here
它当前不计算空行。 它还受 8 KB 的批处理文件行长度限制的约束。
要仅读取(文本(文件的第一行:
<file.txt set /p line=
echo %line%
这是最简单的解决方案。根据内容(毒字符(,echo
可能会失败。