5

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.

1
  • is this windows or unix? Commented Jun 20, 2011 at 10:34

7 Answers 7

7

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 
Sign up to request clarification or add additional context in comments.

1 Comment

@jep, how can eol be set to line-feed?
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

5 Comments

+1, You are faster than me, but why you set the delims to ` and n`?
oops, I removed "\n". Thanks for notice.
Btw, your version removes all ! and sometimes the ^ from the output line
Hmm, I can't understand "why this happen".
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
1

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

1 Comment

Thanks for naming the findstr ! It's quite useful for another task - when the position in the file is not that important, but it's important to find certain line by known prefix or part of content (e.g. with RegEx).
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.

Comments

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

Comments

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 

Comments

-3

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

1 Comment

The question is tagged as [batch-file], so your solution doesn't fit

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.