LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How To Trim A File (http://www.linuxquestions.org/questions/programming-9/how-to-trim-a-file-395120/)

fpfernando 12-21-2005 12:19 AM

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

vladmihaisima 12-21-2005 02:46 AM

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.

kshkid 12-21-2005 04:17 AM

here is a simpler way

Code:

sed -e '$d;1d' <filename>

fpfernando 12-29-2005 11:08 PM

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..............

Hko 12-30-2005 04:00 AM

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.

/bin/bash 12-30-2005 09:27 AM

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.

eddiebaby1023 12-31-2005 10:24 AM

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.

/bin/bash 12-31-2005 04:05 PM

A quick check of the man page will clear it up... maybe not.

fpfernando 01-05-2006 04:58 AM

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.........

fpfernando 01-05-2006 05:07 AM

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

bigearsbilly 01-05-2006 07:41 AM

perhaps you can ask a magic pixie to copy the fie without using any storage or CPU?

graemef 01-05-2006 08:04 AM

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


All times are GMT -5. The time now is 09:54 PM.