6

My text file include with 23 lines (lines include: !@$:/;" )

How can i only display line 3? in or 7? or 19?

I tried all commands was in stackoverflow

Example:

setlocal enabledelayedexpansion
@echo off
for /f "delims=" %%i in (mytext.txt) do (
if 1==1 (
set first_line=%%i
echo !first_line!
goto :eof
))

that's only show first line

Alexander
  • 63
  • 1
  • 1
  • 5
  • The need to just display a line at the prompt from a batch file is very rare, could you explain what exactly you are intending to do with the returned line. – Compo May 23 '18 at 14:52
  • On a somewhat unrelated note, your `if` statement is redundant; it will always return true and the `1` has nothing to do with it being the first line. – SomethingDark May 23 '18 at 15:15
  • 2
    Possible duplicate of [Echo the nth line from a text file where 'n' is a command line argument](https://stackoverflow.com/questions/6409869/echo-the-nth-line-from-a-text-file-where-n-is-a-command-line-argument) – phuclv May 23 '18 at 16:55
  • 1
    quite a lot of duplicates: [Extract specific line from text script](https://stackoverflow.com/q/37683379/995714), [How to make a batch file read one specific line in a text file](https://stackoverflow.com/q/36023698/995714), [Read specific line, and save a specific string in that line as a variable](https://stackoverflow.com/q/38386636/995714), [Batch to Print specific lines of text to a file](https://stackoverflow.com/q/45870979/995714) – phuclv May 23 '18 at 16:56
  • How big is the text file, and how long is the longest line? – aschipfl May 23 '18 at 18:20

4 Answers4

8

@Compo has given a good answer. This is only to expound on it. Using aliases like GC should not be put into scripts. At the command line, sure, go ahead and reduce typing if you feel like it. Also, spelling out the parameter names provides more information and aids faster understanding.

To get only line 3.

GC .\mytext.txt -T 3|Select -L 1

Get-Content -Path '.\mytext.txt' -TotalCount 3 | Select-Object -Last 1

From the CMD console (Command prompt): (to get only line seven (7)

PowerShell "GC .\mytext.txt -T 7|Select -L 1"

PowerShell -NoProfile "Get-Content -Path '.\mytext.txt' -TotalCount 7 | Select-Object -Last 1"

To get lines 3 through 7:

$FirstLine = 3
$LastLine=7
powershell -NoProfile -Command "Get-Content -Path '.\t.txt' -TotalCount $LastLine | Select-Object -Last ($LastLine - $FirstLine + 1)"

Or, in a cmd.exe batch script.

SET "FIRSTLINE=3"
SET "LASTLINE=7"
powershell -NoProfile -Command ^
    "Get-Content -Path '.\t.txt' -TotalCount %LASTLINE% |" ^
        "Select-Object -Last (%LASTLINE% - %FIRSTLINE% + 1)"
lit
  • 14,456
  • 10
  • 65
  • 119
  • When invoking PowerShell from a cmd.exe batch script, as in your last example, if a line is more than 80 characters, a line break is automatically inserted at position 80 in the output. How can I avoid this? In my particular use-case I am piping the output of the PowerShell command to CLIP and do not want an added line break when I paste. – thecommexokid Jan 09 '19 at 16:23
  • 1
    I do not seem to have that result on Windows 7 Enterprise SP1 using PowerShell 5.1.14409.1012. – lit Jan 09 '19 at 18:31
3
@echo off
setlocal
set "FILE_TO_PROCESS=%~f1"
set /a LINE_NUMBER=%~2
set /a trim=LINE_NUMBER-1

break>"%temp%\empty"&&fc "%temp%\empty" "%FILE_TO_PROCESS%" /lb  %LINE_NUMBER% /t |more +4 | findstr /B /E /V "*****"|more +%trim%
endlocal

try with this bat (called lineNumber.bat) the first argument is the file you want to process the second is the line number:

call lineNumber.bat someFile.txt 5
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • 1
    Wow, that is some cmd Fu! Thanks for ending my struggle to get findstr to work. I wish I could figure out how to mix that magic with getting the last word (perhaps some variant of `for /f "tokens=2" %%a in (state.txt) do set word1=%%a`) – deasserted Aug 28 '20 at 01:40
  • @deasserted - you want the last word of line? That can be achieved too though it wont be so convenient to answer you in a comment. – npocmaka Aug 28 '20 at 12:43
  • @deasserted - is this what bothers you? https://stackoverflow.com/questions/60675577/git-parse-branch-name-from-console-output – npocmaka Aug 28 '20 at 12:44
  • Yes, I used your method to address that question. I finally used your solution, read it back in with ```set /p line=<"%temp%\empty"```, and then got the last phrase using ```for %%a in (!line!) do set last_phrase=%%a``` with EnableDelayedExpansion. Thanks! – deasserted Sep 02 '20 at 17:22
  • Just to help others understand what was done in this answer: the break> part creates an empty file (could be its own line). It is used with the FC (file compare) utility to crop all the lines after /lb LINE_NUMBER. It works but its report needs cleanup: so MORE +4 crops the FC results before LINE_NUMBER, and findstr finds the lines without ***** in them. I'm not sure why the last MORE is needed, but I assumed it handles a corner case. If you want to save the result, tack on >my_filename to store it, then ```set /p my_line=<"my_filename "``` to read it back in. – deasserted Sep 02 '20 at 17:41
1

There are a couple of ways to do this. Your first option is to go through the for loop normally and break out of the loop once you reach the desired line.

@echo off

:: Specify which line to return
set get_line=7

:: Skip all lines before it, then print the next line and abort
set /a get_line-=1
for /F "skip=%get_line% delims=" %%A in (mytext.txt) do (
    echo %%A
    goto :end_loop
)

:end_loop

Your other option is to store all lines before the unwanted line in a temp variable and then display the next line.

@echo off
setlocal enabledelayedexpansion

:: Specify which line to return
set get_line=7

:: Skip all lines before it, then print the next line and abort
set /a get_line-=2

(
    for /L %%A in (0,1,%get_line%) do set /p skip_line=
    set /p return_line=
) <file.txt

echo !return_line!

Note that the first option is not suited for returning the first line of the script.

SomethingDark
  • 13,229
  • 5
  • 50
  • 55
0

Since you said 'display', why not give PowerShell a shot:

From the PowerShell console:

GC .\mytext.txt -T 3|Select -L 1

From the CMD console (Command prompt):

PowerShell "GC .\mytext.txt -T 7|Select -L 1"

From a batch file:

@(PowerShell "GC .\mytext.txt -T 19|Select -L 1"&Pause)
Compo
  • 36,585
  • 5
  • 27
  • 39