LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 12-21-2005, 12:19 AM   #1
fpfernando
LQ Newbie
 
Registered: Oct 2005
Distribution: RH 9
Posts: 24

Rep: Reputation: 15
How To Trim A File


My situation is like this............

i have to trim a file at start and end...

but i musn't copy the file to a temp and rename is there anyother way so that i can do it in the same file

cause copyin to a temp file is takin much CPU time..........

Reply ASAP

Thanx

Prashanth.J
 
Old 12-21-2005, 02:46 AM   #2
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
Please consider that by inserting things like "ASAP" and "important" you can annoy people and made them not respond to your post.

And specifify the problem completly. (for example is it a text or a binary file ? if text do you want to trim with a number of lines ?)

If you have a file t.c with 5 lines you can use :

Code:
cat t.c  | head -n 4 | tail -n 3
to trim the first and the last line.
 
Old 12-21-2005, 04:17 AM   #3
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
here is a simpler way

Code:
sed -e '$d;1d' <filename>
 
Old 12-29-2005, 11:08 PM   #4
fpfernando
LQ Newbie
 
Registered: Oct 2005
Distribution: RH 9
Posts: 24

Original Poster
Rep: Reputation: 15
First of all Im sorry.............


Im recording a m2v file and my requirement is to trim the file provided i get the start and end offset of the file.

earlier i was doing was like this i go to the particular offset and copy the rest to a temp file and atlast rename that temp file.But CPU utilisation is high....... so im looking for a alternative method..............
 
Old 12-30-2005, 04:00 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Are you looking for a shell script, C, C++, python program?
Could you post the code that has the high CPU utilization, so we can see if it can be improved? When removing (or inserting) bytes from the start of a file, you won't get around rewriting the entire file. So the high CPU utilization is there to stay, or the performance of your code can be improved.
 
Old 12-30-2005, 09:27 AM   #6
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
How about this?

nice -19 dd if=my_file.d2v of=my_trimmed_file.d2v bs=1 skip=XXX count=YYY

XXX will trim off the front of the file then YYY is the lenght of the file.
NOTE: nice will reduce the CPU drain for any command you want to use.
 
Old 12-31-2005, 10:24 AM   #7
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
NOTE: nice will reduce the CPU drain for any command you want to use.
How? If he needs to use CPU to run the command, running it at a higher priority won't make it use less it'll just take more away from other processes.
 
Old 12-31-2005, 04:05 PM   #8
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
A quick check of the man page will clear it up... maybe not.

Last edited by /bin/bash; 12-31-2005 at 04:12 PM.
 
Old 01-05-2006, 04:58 AM   #9
fpfernando
LQ Newbie
 
Registered: Oct 2005
Distribution: RH 9
Posts: 24

Original Poster
Rep: Reputation: 15
This is my code which i have written in C. TYhis is just a simple fseek read and write

i estimate the file size by knowing both strat and end offset.
and the rest the code explains hope u understtod it .... is there any way to cut short this............

filesize = off_ep - off_sp; //Actual file size
fdr = fopen(oldfile,"r");
if (fdr == NULL)
{
err_no = errno;
syslog(LOG_ERR,"%s:%s",oldfile,strerror(err_no));
return -1;
}

if(fseek(fdr,off_sp,SEEK_SET)<0){
err_no = errno;
//perror("failed to seek\n");
syslog(LOG_ERR,"failed to seek");
syslog(LOG_ERR,"failed to seek3:%s",strerror(err_no));
return -1;
}


sprintf(newfile,"%s/%s.m2v",FILEPATH,&shortname[1]);
fdw = fopen(newfile,"w");
if (fdw == NULL)
{
err_no = errno;
//perror(newfile);
syslog(LOG_ERR,"%s:%s",newfile,strerror(err_no));
return -1;
}
fptr=0;
while(fptr < filesize)
{
if((rc=fread(buf,1,128*1024,fdr))<0)
{
err_no = errno;
//perror("Input file");
syslog(LOG_ERR,"Input file:%s",strerror(err_no));
break;
}
if(fwrite(buf,1,rc,fdw)<0)
{
err_no = errno;
//perror("Output file");
syslog(LOG_ERR,"Output file",strerror(err_no));
break;
}
fptr+=rc;
}
fclose(fdr);
fclose(fdw);
free(buf);
}



and atlast rename the temp file as the orginal file.........
 
Old 01-05-2006, 05:07 AM   #10
fpfernando
LQ Newbie
 
Registered: Oct 2005
Distribution: RH 9
Posts: 24

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by /bin/bash
How about this?

nice -19 dd if=my_file.d2v of=my_trimmed_file.d2v bs=1 skip=XXX count=YYY

XXX will trim off the front of the file then YYY is the lenght of the file.
NOTE: nice will reduce the CPU drain for any command you want to use.

this too will and was utilizing 80 - 90 % of CPU
 
Old 01-05-2006, 07:41 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,269

Rep: Reputation: 165Reputation: 165
perhaps you can ask a magic pixie to copy the fie without using any storage or CPU?
 
Old 01-05-2006, 08:04 AM   #12
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
Please can you use the [CODE] tags when displaying code, I for one can't read left aligned code (maybe its the fading eyesight )

here is an outline of the design you may need
  1. You will need a character array of end - start bytes.
  2. Open the file for read
  3. Then read in the first part of the file in blocks of (end - start) bytes up to the start offset
  4. read in the required file length
  5. Close the file for read
  6. Open the file for write
  7. Write out the required file length
  8. close the file

You will end up reading the file from the start to the end offset which is not quite as efficient as the magic pixie approach but easier to implement!

graeme
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
trim a line schneidz Programming 2 10-07-2005 06:47 PM
Java Problem on trim command k1ll3r_x Programming 2 09-12-2005 03:49 PM
I want to trim Linux distribution qadria Linux - Software 7 08-08-2005 07:45 AM
How to Trim mpeg video's ? qwijibow Linux - Software 1 01-06-2004 08:28 PM
How can I safely trim down /dev ? tigermusti Linux - General 9 12-12-2002 04:33 PM


All times are GMT -5. The time now is 11:46 PM.

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