8

I have a unix command that does the following:

head -c 2048 > test.txt

basically its taking first 2kb of the test.txt file.

Can we do something similar in windows cmd prompt?

  • thank you guys for all your awesome replies..@sonjz I have a text file with one long string of hex dump..how can i use your code to extract only 2kb worth of data from it?? I tried your code but its still extracting the whole hex dump..thanks..i will upload the text file file for you.. – plasma33 Jan 3 '15 at 5:41
  • please find the link here-> dropbox.com/s/h29mdg0t8df8i1e/test.txt?dl=0 – plasma33 Jan 3 '15 at 5:52
  • @ sonjz: how to use "Get-FileHex" to extract 2kb of hex data using powershell..do you know about this function?? – plasma33 Jan 3 '15 at 6:21
  • you want to extract byte array. The following code will yank 2KB and write to a new file: $byteArray = Get-Content binary.bin -Encoding Byte -TotalCount 2KB; [io.file]::WriteAllBytes('c:\binary.bin', $byteArray) – sonjz Jan 6 '15 at 1:03
10

Simplifying this answer because of @chubbsondubs' comment.

-TotalCount will count lines if reading in text, so always force it to read the file as bytes, then the -TotalCount will only refer to bytes and you can get an account count.

Get-Content test.txt -Encoding byte -TotalCount 2KB | Set-Content test1.txt -Encoding byte

More information here: https://stackoverflow.com/questions/888063/powershell-to-get-the-first-x-mb-of-a-file

  • This gets the first 2048 lines not bytes of the file. TotalCount takes the number of lines you want. – chubbsondubs Jun 26 '18 at 14:40
  • @chubbsondubs yup, i see the issue now, you always have to encode and decode in bytes, i'll update the answer – sonjz Jun 27 '18 at 1:11
  • this is not the command prompt, its powershell – user32882 Mar 9 at 4:54
  • Question was for "command line", both command prompt and PowerShell are command line shells for Windows. Command prompt being the archaic "stop using this" one. – sonjz Mar 10 at 22:07
1

From what I can tell you can't print out by size in a native way; there is the type command which will output a whole text file, but you can't specify how much you want to output.

There is also the more command, which will allow you to print out lines of a file. These are some of the flags from more /?:

/E      Enable extended features
/C      Clear screen before displaying page
/P      Expand FormFeed characters
/S      Squeeze multiple blank lines into a single line
/Tn     Expand tabs to n spaces (default 8)

        Switches can be present in the MORE environment
        variable.

+n      Start displaying the first file at line n

files   List of files to be displayed. Files in the list
        are separated by blanks.

If extended features are enabled, the following commands
are accepted at the -- More -- prompt:

P n     Display next n lines
S n     Skip next n lines
F       Display next file
Q       Quit
=       Show line number
?       Show help line
<space> Display next page
<ret>   Display next line

If neither of these work for you, you can alternatively install Cygwin and you can use cat or head.

  • cygwin also has the 'head' command if you choose to use that option – Jeff Clayton Jan 3 '15 at 3:41
  • @JeffClayton Shoot.. you're right, for some reason I was thinking cat instead of head – cutrightjm Jan 3 '15 at 3:43
  • 'cat' is the unix command that matches up to the 'type' command in windows, since you were discussing 'type' it only makes sense. – Jeff Clayton Jan 3 '15 at 3:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.