4

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.

oliholz
  • 7,447
  • 2
  • 43
  • 82
Marcos
  • 41
  • 1
  • 1
  • 2

7 Answers7

6

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
jeb
  • 78,592
  • 17
  • 171
  • 225
1

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

mattn
  • 7,571
  • 30
  • 54
  • +1, You are faster than me, but why you set the delims to `\` and `n`? – jeb Jun 20 '11 at 11:21
  • oops, I removed "\n". Thanks for notice. – mattn Jun 20 '11 at 11:23
  • Btw, your version removes all `!` and sometimes the `^` from the output line – jeb Jun 20 '11 at 11:34
  • Hmm, I can't understand "why this happen". – mattn Jun 20 '11 at 11:40
  • The `!` are removed as the delayed expansion is enabled, only with disabled expansion you can safely access `%%1` see also [Script to remove special characters from filenames](http://stackoverflow.com/questions/4429604/script-to-remove-special-characters-from-filenames/4448017#4448017) – jeb Jun 20 '11 at 11:48
0

+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.

Seth McCauley
  • 983
  • 11
  • 24
0

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

Amit
  • 1
0

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
thebunnyrules
  • 1,520
  • 15
  • 22
0

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.

Andriy M
  • 76,112
  • 17
  • 94
  • 154
-3

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

uddeep
  • 1