- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: strip characters from file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2007 06:09 AM
06-04-2007 06:09 AM
strip characters from file
Mike
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2007 06:31 AM
06-04-2007 06:31 AM
Re: strip characters from file
$ dd if=foo of=foo_stripped bs=1 skip=10
will strip the first 10 bytes from foo and save the result to foo_stripped.
PCS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2007 06:53 AM
06-04-2007 06:53 AM
Re: strip characters from file
dd if=foo of=foo_stripped bs=1 skip=10,
will work, it's going to be slow as molasses if the file is anything but tiny in size because the blocksize is so small.
I would break this into 2 pieces; one using the small bs and a second using a larger blocksize.
dd if=infile bs=1 skip=10 count=1014 of=outfile
dd if=infile bs=1k skip=1 >> outfile
This will read the first 1KiB of the file (10 + 1014 bytes) and then switch to a larger blocksize (1Kib) for the remainder of the file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2007 07:54 AM
06-04-2007 07:54 AM
Re: strip characters from file
Another way:
$ typeset -i SKIP=10
$ tail -c $(( $(cat foo | wc -c)-${SKIP} )) foo > foo_stripped
PCS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2007 10:28 AM
06-04-2007 10:28 AM
Re: strip characters from file
This Perl script will strip the first 10-bytes, updating "inplace" the file specified as an argument to the script:
# cat ./strip
#!/usr/bin/perl
use strict;
use warnings;
my $file = shift or die "Usage: file\n";
open( FH, "+<:raw", $file ) or die "Can't open $file: $!\n";
local $/ = undef;
$_ =
seek( FH, 0, 0 );
truncate( FH, 0 );
s/.{10}//s; #...strip 10-bytes...
print FH;
close FH;
1;
...run as:
# ./strip myfile
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2007 12:48 PM
06-04-2007 12:48 PM
Re: strip characters from file
A minor improvement might be to use:
dd if=foo of=foo_stripped bs=10 skip=1
... but some dd implementations are not happy with partial blocks at the end.
JRF,
I think that'll work on HPUX, which is the platform of choice here, but will fail on Windoze. Unless in binmode the Windoze the CR-LF line terminators will be presented as single newlines to the script and expanded back out on output. Thus a random binary single byte newline will become two byte.
I would suggest simple copy loop in any language willing to go binary.
In perl that could look like:
#!/usr/bin/perl
use strict;
use warnings;
my $OFFSET = 10;
my $BUFLEN = 8192;
my ($input_file, $output_file, $bytes, $total, $buffer);
$input_file = shift or die "Please provide input and output file name";
$output_file = shift or die "Please provide output file name";
open (IN, "<$input_file") or die "Failed to open $input_file for input";
open (OU, ">$output_file") or die "Failed to open $output_file for output";
binmode IN;
binmode OU;
$bytes = sysread IN,$buffer,$BUFLEN;
$total = ($bytes)? $bytes : 0;
while ($bytes) {
syswrite OU,$buffer,$BUFLEN,$OFFSET;
$bytes = sysread IN,$buffer,$BUFLEN;
if (defined $bytes) {
$total += $bytes;
} else {
print STDERR "Error reading $input_file: $!";
}
}
print STDERR "$total bytes read.";
hth,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2007 04:29 PM
06-04-2007 04:29 PM
Re: strip characters from file
Yes, I found that out years ago. I wanted a piece out of the middle. So I used tail and I had to write a ftruncate program.
>PCS: $ typeset -i SKIP=10
$ tail -c $(( $(cat foo | wc -c)-${SKIP} )) foo > foo_stripped
Since tail doesn't work more than 20Kb (fixed in 11.11), you should use tail +:
$ tail -c+$(($SKIP+1)) foo > foo_stripped
Note: There is no reason to use cat above, use:
$(wc -c < foo)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2007 12:40 AM
06-05-2007 12:40 AM
Re: strip characters from file
> Hein: JRF, I think that'll work on HPUX, which is the platform of choice here, but will fail on Windoze. Unless in binmode the Windoze...
Yes, I agree, but my understanding (assuming a current Perl version --- and that may be a poor assumption!) is that the specification of the 'raw' layer in the open() is equivalent:
http://perldoc.perl.org/PerlIO.html
Regards!
...JRF...