开发者

Echo the nth line from a text file where 'n' is a command line argument

开发者 https://www.devze.com 2023-03-13 22:19 出处:网络
I have a simple text file with numbers like: 12345 45678 34567 89101 I need a batch that will return the nth line from this file. n should be taken from a command line argument.

I have a simple text file with numbers like:

12345
45678
34567
89101

I need a batch that will return the nth line from this file. n should be taken from a command line argument.

I am very new to开发者_开发知识库 batch scripting so Thanks in advance for any help on this.


To get the file from the nth line you could use more +n (For line1 is n=0).
To split the rest of the file you could use a FOR /F loop.

This works even, if there are empty lines before the nth line.
It could be necessary to set the EOL to an unused character or to linefeed (default is ;)

set "lineNr=%1"
set /a lineNr-=1
for /f "usebackq delims=" %%a in (`more +%lineNr% text.txt`) DO (
  echo %%a
  goto :leave
)
:leave


You can use batch file extension.

token.bat

 @echo off

 setlocal ENABLEDELAYEDEXPANSION
 set l=%1
 set c=0
 for /f "delims=" %%1 in ('type foo.txt') do (
   set /a c+=1 && if "!c!" equ "%l%" echo %%1%
 )

If you have a file like following,

foo.txt

 AAAA
 BBBB
 CCCC
 DDDD

And specify line number like following

 token 3

You'll get

CCCC


You could use FOR /F with the skip parameter:

@ECHO OFF
SET skip=%1
SET /A skip-=1
IF %skip% LSS 0 GOTO out
IF %skip% GTR 0 SET params="skip=%skip%"
FOR /F %params% %%L IN (filename) DO (SET "line=%%L"& GOTO out)
:out
ECHO %line%

The skip parameter means the FOR /F loop must skip the specified number of lines at the beginning. The parameter is only applied if you specify a line number greater than 1. If you specify a number less than one or a non-number, the script outputs an empty string.


+1 for Jeb's solution... I did not realize you could use the more command to skip lines like that!

Here is an alternate method that I use for getting a specific line from a file (or from the multi-line output of another program):

@echo off
for /f "tokens=1* delims=:" %%a in ('findstr /n .* "C:\SomeFile.txt"') do (
  if "%%a" equ "%1" echo.%%b
)

I use findstr /n .* "Path\FileName.ext" to add line numbers, and to ensure no empty lines are skipped by the for loop.

I then set "tokens=1* delims=:" to separate the line numbers from the line content.

Finally, I compare the current line number (%%a) with the line specified by the %1 parameter, and echo the line contents (%%b) on a match.


To Find Nth to Mth Character In Line No. L --- Example For Finding Label


@echo off

REM Next line = Set command value to a file  OR  Just Choose Your File By Skipping The Line
vol E: > %temp%\justtmp.txt
REM  Vol E:  = Find Volume Lable Of Drive E

REM  Next Line to choose line line no. +0 = line no. 1 
for /f "usebackq delims=" %%a in (`more +0 %temp%\justtmp.txt`) DO (set findstringline=%%a& goto :nextstep)

:nextstep

REM  Next line to read nth to mth Character  here 22th Character to 40th Character
set result=%findstringline:~22,40%

echo %result%
pause
exit /b

Save as find label.cmd

The Result Will Be Your Drive E Label

Enjoy


Based off of Amit's Answer, I made a utility called snip.cmd.

USAGE: snip FILE LINE FIRSTCHARACTER LASTCHARACTER

example:

foo.txt

    //
    // File Checksum Integrity Verifier version 2.05.
    //
    77752e4c91ba96dcc6c2bb4bdcdbdec5 c:\users\lapinot\desktop\token.bat

snip foo.txt 3 0 33 will yield 77752e4c91ba96dcc6c2bb4bdcdbdec5

Here is my code for snipe.cmd (you can add snipe to your command line by copying snip.cmd to c:\Windows\System32):

    @echo off
    :: USAGE
    :: snip <file> [Line - Starts at 0] [First Column - Start Count at 0] [Last Colum]


    set file=%1
    set line=%2
    set char1=%3
    set char2=%4

    for /f "usebackq delims=" %%a in (`more +%line% %file%`) DO (set findstringline=%%a& goto :nextstep)

    :nextstep
    echo echo %%findstringline:^~%char1%,%char2%%% > %temp%\result.bat
    %temp%\result.bat
    del %temp%\result.bat
    :EOF
    exit /b


echo "enter the line number.."; read i; awk 'NR==$i' <file_name>;

0

精彩评论

暂无评论...
验证码 换一张
取 消