Get file's first x bytes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get file's first x bytes
# 1  
Old 05-10-2013
Get file's first x bytes

is there a better way to do this:
Code:
head -c 10000k /var/dump.log | head -c 6000k

unfortunately, the "-c" option is not available on sun solaris. so i'm looking at "dd". but i dont know how to use it to achieve the same exact goal as the above head command.

this needs to work on both solaris and linux systems.
# 2  
Old 05-10-2013
Quote:
Originally Posted by SkySmart
is there a better way to do this:
Code:
head -c 10000k /var/dump.log | head -c 6000k

unfortunately, the "-c" option is not available on sun solaris. so i'm looking at "dd". but i dont know how to use it to achieve the same exact goal as the above head command.

this needs to work on both solaris and linux systems.
I can supply a translation from head to dd, but I need to understand why you used the above command line on Linux rather than just:
Code:
head -c 6000k /var/dump.log

and I need to understand what -c 6000k means to you. The Linux head man page defines the meaning for 6000kB and for 6000K, but it does not specify any meaning for 6000k???
# 3  
Old 05-10-2013
Try this:
Code:
head -1 file | cut -c1-5

Print 1st 5 bytes of first record.
# 4  
Old 05-10-2013
Quote:
Originally Posted by Don Cragun
I can supply a translation from head to dd, but I need to understand why you used the above command line on Linux rather than just:
Code:
head -c 6000k /var/dump.log

and I need to understand what -c 6000k means to you. The Linux head man page defines the meaning for 6000kB and for 6000K, but it does not specify any meaning for 6000k???
it means nothing to me. please show me your suggested solution.
# 5  
Old 05-10-2013
If you don't know what you want, I have no idea whether what I suggest will succeed. I am guessing that the intent of the pipeline:
Code:
head -c 10000k /var/dump.log | head -c 6000k

is to extract the first 10000*1000 bytes from /var/dump.log and then extract the first 6000*1000 bytes from that. That is equivalent to extracting the first 6000*1000 bytes from /var/dump.log. If you had specified 10000kB and 6000kB, that is what would be done according to the Linux head man page. If you had used 10000K and 6000K, the multiplier would be 1024 instead of 1000 according to the Linux head man page. If the count had been negative in the 2nd call to head, the command line would make more sense since it would then be getting the last 6000*(1000 or 1024) bytes from the first 10000*(1000 or 1024) bytes. But, I can only work with the command line you supplied and guess at the multiplier (or block size) you want. So:
Code:
dd if=/var/dump.log count=6000 bs=1000

might do what you want. If you want a block size of 1024 instead of 1000, change the bs=1000 to bs=1024.

If wanted the last 6000 blocks of the first 10000 blocks from the file, you could try:
Code:
dd if=/var/dump.log skip=4000 count=6000 bs=1000

This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 05-10-2013
Here are some examples of using 'dd' to accomplish what you are asking:
Code:
# dd outputs 1st 5 bytes of file to screen with diagnostic info
dd if=input_file bs=1 count=5
# dd outputs 1st 5 bytes of file to screen with diagnostic info going to file
dd if=input_file bs=1 count=5 2>test.txt
# dd outputs 1st 5 bytes of file to file with diagnostic info going to file
dd if=input_file bs=1 count=5 of=output_file 2>test.txt

This User Gave Thanks to spacebar For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script - entered input(1-40 bytes) needs to be converted exactly 40 bytes

hello, suppose, entered input is of 1-40 bytes, i need it to be converted to 40 bytes exactly. example: if i have entered my name anywhere between 1-40 i want it to be stored with 40 bytes exactly. enter your name: donald duck (this is of 11 bytes) expected is as below - display 11... (3 Replies)
Discussion started by: shravan.300
3 Replies

2. UNIX for Dummies Questions & Answers

X bytes of 0, Y bytes of random data, Z bytes of 5, T bytes of 1. ??

Hello guys. I really hope someone will help me with this one.. So, I have to write this script who: - creates a file home/student/vmdisk of 10 mb - formats that file to ext3 - mounts that partition to /mnt/partition - creates a file /mnt/partition/data. In this file, there will... (1 Reply)
Discussion started by: razolo13
1 Replies

3. Programming

Copying 1024 bytes data in 3-bytes chunk

Hi, If I want to copy a 1024 byte data stream in to the target location in 3-bytes chunk, I guess I can use the following script. dd bs=1024 count=3 if=/src of=/dest But, I would like to know, how to do it via a C program. I have tried this with memcpy(), that did not help. (3 Replies)
Discussion started by: royalibrahim
3 Replies

4. Shell Programming and Scripting

Error PHP Fatal error: Allowed memory size of 67108864 bytes exhausted(tried to allocate 401 bytes)

While running script I am getting an error like Few lines in data are not being processed. After googling it I came to know that adding such line would give some memory to it ini_set("memory_limit","64M"); my input file size is 1 GB. Is that memory limit is based on RAM we have on... (1 Reply)
Discussion started by: elamurugu
1 Replies

5. Shell Programming and Scripting

Capture first N Bytes from first line in a file

Hi Guyz, I need to capture first N Bytes from the first line of my file. Eg. If i have following data in File1 414d51204541495052475731202020204a910846230e420c Hello 3621363663212 Help Required Then, i want the value of first 48 Bytes to be stored in a variable. That is, variable... (5 Replies)
Discussion started by: DTechBuddy
5 Replies

6. Programming

how to inspect the bytes in a file?

What is the easiest way to inspect the bytes stored in a file? Ideally, If my file was 10 bytes each of which had only the high bit set, I'd be able to browse for it and get output like this: 01 - 10000000 02 - 10000000 03 - 10000000 04 - 10000000 05 - 10000000 06 - 10000000 07 -... (7 Replies)
Discussion started by: sneakyimp
7 Replies

7. UNIX for Dummies Questions & Answers

Bytes of character in file

Hi, How do I check for the total bytes of character used by a file? Can I used a od command to check? Thanks. (1 Reply)
Discussion started by: user50210
1 Replies

8. Shell Programming and Scripting

Remove first N bytes and last N bytes from a binary file on AIX.

Hi all, Does anybody know or guide me on how to remove the first N bytes and the last N bytes from a binary file? Is there any AWK or SED or any command that I can use to achieve this? Your help is greatly appreciated!! Best Regards, Naveen. (1 Reply)
Discussion started by: naveendronavall
1 Replies

9. Shell Programming and Scripting

delete bytes from file

I'm doing a bit of hex editing with dd and I can replace values fairly simply. However, I've run across a situation where I need to delete bytes in the file and I'm not sure how to do that. For example: Input file has: 1234567890 Output needs to be: 123abc90 I tried this: printf... (6 Replies)
Discussion started by: Loriel
6 Replies

10. UNIX for Dummies Questions & Answers

Take a file from the system and put on tape and reset the file to 0 bytes

:mad: I did this the other day but one of my support personnel removed my history so i could call it back up to remeber the exact command since i am air-headed at times. I am trying to take a 30 MEG file off the system and drop it to tape then i want to make the file go back to being 0 bytes so... (1 Reply)
Discussion started by: JackieRyan26
1 Replies
Login or Register to Ask a Question