user352156
user352156

Reputation: 99

Get the first two string of a text file in batch files

I have a text file that contains data like this but I want to only get the first two strings of the file. from this:

Caption                           
SMI USB DISK USB Device    
Hitachi HTS547550A9E384  

to this:

Caption                      
SMI USB DISK USB Device

Upvotes: 0

Views: 592

Answers (2)

Monacraft
Monacraft

Reputation: 6620

Very simple:

@echo off
3<text.txt (
set /p line1= <&3
set /p line2= <&3
)
Echo %line1%
Echo %line2%

Which is how you get the first to lines of a file. To actually change the file so it only consists of these two lines use:

@echo off
3<text.txt (
set /p line1= <&3
set /p line2= <&3
)
Echo %line1% > text.txt
Echo %line2% >> text.txt

Note: You have to replace text.txt with the name of your file.

Upvotes: 3

Szymon Roziewski
Szymon Roziewski

Reputation: 1145

If you have powershell, you can make it by

powershell -command "& {get-content filename -totalcount n}"

Where n is the number of lines to display.

Upvotes: 0

Related Questions