Delete specific lines in a text file


 
Thread Tools Search this Thread
Top Forums Programming Delete specific lines in a text file
# 1  
Old 07-18-2005
Error Delete specific lines in a text file

Hi, experts,

I would like to create a function that can calculate the total number of lines in a saved text file and delete specific lines in that particular file (I only want the last few lines). Hav anybody have the experience and giv me a hand in this?
# 2  
Old 07-18-2005
Do you want it the C way or the shell script way ?

Shell script way is quite easy as compared to the C way

Vino
# 3  
Old 07-18-2005
maybe you are looking for this:
display the last 50 lines of something.txt
Code:
tail -n50 something.txt

there is also 'head' which displays the first few lines.
# 4  
Old 07-18-2005
Hi,

I actually wanted to do it the C way. Yeah, it would be a lot easier using shell Smilie My attempt is to keep only the final 100 lines in a text file, and throw the rest of the content, not meant for display only. I've searched through some books and website and found nothing so far.
# 5  
Old 07-19-2005
Err well, typically you would identify a line by the presence of the '\n' character.

So, open the file. Go the end of the file. Work backwards towards the start of the file, counting the number of '\n'. When you have reached the required number of '\n' characters, store the position in the file.

Read from the stored position of the file to the end of file into memory buffer. Dump buffer contents into file.

Or something like that ...

Some useful functions are fopen, fclose, fread and fwrite.

Hope this helps.

MBB
# 6  
Old 07-19-2005
This is a start
Code:
/* last100 */
/* usage - last100 <filename> lines_to_keep */
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

typedef int fildes;
/* read a buffer */
ssize_t readall(fildes fd, void *buf, size_t nbyte){
     ssize_t nread = 0;
     ssize_t n=0;

     do {
         if ((n = read(fd, &((char *)buf)[nread], nbyte - nread)) == -1) {
             if (errno == EINTR)
                 continue;
             else
                 return (-1);
         }
         if (n == 0)
             return nread;
         nread += n;
     } while (nread < nbyte);
     return nread;
}

char  *process_file(fildes in, char *buf, size_t *len)
{
	struct stat st;
    if (fstat(in,&st)==(-1) )          /* get file size in bytes */
    {
        perror("Cannot stat file");    /* error */
        exit(EXIT_FAILURE);
    }
    *len=(size_t)st.st_size+1;
    buf=malloc((size_t)st.st_size+1);          /* allocate memory for whole file */
    if(buf==NULL)
    {
        perror("Memory allocation failure");	/* mem error */
        exit(EXIT_FAILURE);
    }
    memset(buf,0x0,(size_t)st.st_size+1);
    if(readall(in,buf,(size_t) st.st_size)==(-1)) /* read in whole file */
    {
        perror("Error on file read");   /* error */
        exit(EXIT_FAILURE);	
    }
	return buf;
}

char *position_ptr(char *p,size_t *len, long linecount)
{
    size_t pos=*len;	
    while (pos && linecount>(-1))
    {
    	if( *(p+pos--)=='\n') linecount--;    	
    }
    p+=pos;
    p++; /* skip first newline */
    *len-=pos;
    return p;
}

int main(int argc, char *argv[])
{
    
    size_t len=0;
    char *buf=NULL;
    char *p=NULL;
    fildes in=open(argv[1],O_RDONLY);  /* low-level file open */
    
    if(in==(-1))                      /* error check */
    {
        perror("Error opening input file");
        exit(EXIT_FAILURE);
    }
    buf=process_file(in,buf,&len);
    p=position_ptr(buf,&len,atol(argv[2]));
    fprintf(stdout,"%s",p);
    free(buf);                        /* clean up and exit */    
    if(close(in))
    {
         perror("File close error");
         exit(EXIT_FAILURE);	
    }    
    return 0;
}

# 7  
Old 07-20-2005
Hi, Jim,

I've tried out the code but it seems the libraries are not compatible to my unix system. It kept errored out with the "Function prototypes are an ANSI feature" and "Incompatible types in cast: Must cast from scalar to scalar or to void type". I am just at the beginner level, perhaps, you can give me a guide of what is it all about? Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete lines above and below specific line of text

I'm trying to remove a specific number of lines, above and below a specific line of text, highlighted in red: <STMTTRN> <TRNTYPE>CREDIT <DTPOSTED>20151205000001 <TRNAMT>10 <FITID>667800001 <CHECKNUM>667800001 <MEMO>BALANCE </STMTTRN> <STMTTRN> <TRNTYPE>DEBIT <DTPOSTED>20151207000001... (8 Replies)
Discussion started by: bomsom
8 Replies

2. Shell Programming and Scripting

Delete specific lines from files based on another file

I have some text files in a folder named ff as follows. I need to delete the lines (in-place editing)in these files based on another file aa.txt. 32bm.txt: 249 253 A P - 0 0 8 0, 0.0 6,-1.4 0, 0.0 2,-0.4 -0.287 25.6-102.0 -74.4 161.1 37.1 13.3 10.9 250... (2 Replies)
Discussion started by: aden
2 Replies

3. Shell Programming and Scripting

How to delete lines of a text file based on another text file?

I have 2 TXT files with with 8 columns in them(tab separated). First file has 2000 entries whereas 2nd file has 300 entries. The first file has ALL the lines of second file. Now I need to remove those 300 lines (which are in both files) from first file so that first file's line count become... (2 Replies)
Discussion started by: prvnrk
2 Replies

4. UNIX for Dummies Questions & Answers

How to delete specific lines (2n+3 line, n=0,1,2...296) in a file?

Dear everyone, I have a file with 900 lines (there is only numbers in one line, no string), I only need the lines 2+3n (n=0,1...296), i.e line 2, 5, 8, 11...888. I tried google but only the results such as how to delete all the odd lines or all the even lines with 'awk' command. Thanks in... (4 Replies)
Discussion started by: phamnu
4 Replies

5. Shell Programming and Scripting

How to delete lines from text file?

hi guys, I have very large txt files (200GB) and just want to to delete the first two lines (headers). So far I used sed -i '1,2d' infile.txtbut this command always takes extremely long as it writes all again. Is there a better way to do it (ie just to delete the lines without writing all... (2 Replies)
Discussion started by: TuAd
2 Replies

6. Shell Programming and Scripting

how to delete lines from a file which starts with a specific pattern

I need to delete those lines from a file, which starts with 45. How to do it? (3 Replies)
Discussion started by: mady135
3 Replies

7. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

8. Shell Programming and Scripting

Delete specific lines from a file

Hi, I have a file ( all_users.ldif ) of the following format: cn=orcladmin, cn=Users, dc=maximus,dc=com cn=PUBLIC, cn=Users, dc=maximus,dc=com cn=portal,cn=users,dc=maximus,dc=com cn=portal_admin,cn=users,dc=maximus,dc=com cn=uddi_publisher,cn=Users,dc=maximus,dc=com... (4 Replies)
Discussion started by: itzz.me
4 Replies

9. Shell Programming and Scripting

Delete lines prior to a specific date in a log file.

Hi all. I have a database log file in which log data get appended to it daily. I want to do a automatic maintainence of this log by going through the log and deleting lines belonging to a certain date. How should i do it? Please help. Thanks. Example. To delete all lines prior to Jun... (4 Replies)
Discussion started by: ahSher
4 Replies

10. Shell Programming and Scripting

Delete blocks of lines from text file

Hello, Hello Firends, I have file like below. I want to remove selected blocks say abc,pqr,lst. how can i remove those blocks from file. zone abc { blah blah blah } zone xyz { blah blah blah } zone pqr { blah blah blah } (4 Replies)
Discussion started by: nrbhole
4 Replies
Login or Register to Ask a Question