LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 11-17-2010, 12:08 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,201

Rep: Reputation: 49
Split a file, byte-wise, not character-wise.


Hi:

I want to split file foo into two parts, and I only intersted in the first one. But the N bytes in which the first part consists, I want it to be an exact replica of the corresponding bytes of foo. More exactly:
Code:
foo        foo1         foo2
==============================
90         90           05
00         00
05
That is, I have foo, and what I want is foo1, foo2 being only the rest of foo. Any hint would be welcome. Regards.
 
Old 11-17-2010, 02:31 AM   #2
rew
Member
 
Registered: May 2010
Posts: 33

Rep: Reputation: 2
There is a program called "split". It will count the number of bytes if you pass it the -b argument.

split -b 2 foo

will work in your case. (but it will continue to create 2-byte files if foo is much larger).

If you only need the first part of foo, you can use:

head -c 2 foo > foo1

If you don't need to keep the old "foo", you can use the truncate system call. I believe there is now a "truncate" program somewhere, but I wrote it for my own use a long time ago.

truncate 2 foo
 
Old 11-17-2010, 02:53 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
You can also use dd. To get the first N bytes into foo1, use:
Code:
dd if=foo of=foo1 ibs=N count=1
to put the rest into file foo2, you have to skip the first N bytes and copy the rest:
Code:
dd if=foo of=foo2 ibs=N skip=1
Pay attention to what you type, especially in writing the input and the output files correctly. See man dd for details about the various operands (if, of, ibs, skip and count).
 
Old 11-17-2010, 04:48 AM   #4
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE 13.1 / 12.3_64-KDE, Ubuntu 14.04, Fedora 20, Mint 17, Chakra
Posts: 3,675

Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
...See man dd for details about the various operands (if, of, ibs, skip and count).
Or go to http://www.linuxquestions.org/questi...d.php?t=362506

 
Old 11-17-2010, 06:43 AM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,201

Original Poster
Rep: Reputation: 49
Thank you guys. I had used 'head' before writing LQ but something went wrong with a calculation I did and as a result I came to conclude head interprets bytes as characters, filtering non printable chars out.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script comparison for two file(text) character wise vaibhavs17 Programming 4 03-01-2010 01:05 PM
how to Block a site in a Firewall or Router by Protocol wise & host wise. shahid khan Linux - Security 2 06-26-2009 04:13 PM
Word to the Wise dfowensby Ubuntu 1 02-08-2008 08:04 AM
bandwidth allocation by user wise and ip wise basbosco Linux - Networking 1 11-12-2003 02:54 AM
is it wise to upgrade to kernel 2.4.21 duister Slackware 26 08-16-2003 03:39 PM


All times are GMT -5. The time now is 01:10 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration