[Courses] How to get line-information from buffer..

sneha sneha at vit.ac.in
Mon Sep 30 13:59:21 EST 2002


** hello :)
        Thank u for correcting me conor :)
?? what if someother line contains the word "Host" and only the first
occurrence is required....

!!  grep "Host" info| head -n 1 | cut -d ":" -f3

friendly
sneha :o)
----- Original Message -----
From: Conor Daly <c.daly at met.ie>
To: Linuxchix <courses at linuxchix.org>
Sent: Monday, September 30, 2002 2:46 PM
Subject: Re: [Courses] How to get line-information from buffer..


> On Mon, Sep 30, 2002 at 11:37:08AM +0530 or thereabouts, sneha wrote:
> >
> >    consider a file named  "info" carries the following information:
> >
> >    GET / HTTP/1.0
> >     Connection: Keep-Alive
> >     User-Agent: Mozilla/4.75 [en] (X11; U; Linux
> >     2.2.17-21mdk i686)
> >     Host: localhost:8000
> >     Accept: image/gif, image/x-xbitmap, image/jpeg,
> >     image/pjpeg, image/png, */*
> >     Accept-Encoding: gzip
> >     Accept-Language: en
> >     Accept-Charset: iso-8859-1,*,utf-8
>
> What if an extra line appears at the top of the file?  You would do better
> to look for the string "Host:" and then parse that line for the details.
>
> >    C code :
> >
> >    ----------
>
> #include<stdio.h>
> #include<string.h>
> main()
> {
> FILE *f1;
> char ch,*str,*found;
> f1=fopen("info","r");
> str=(char *) calloc(sizeof(char *),80);
> while(( fgets(str,80,f1) !=EOF)
> {
> if( ( found=strstr(str,"Host:") ) != NULL )
>         break;
> }
> printf("%s",str);
> }
>
>
> >    Shell Script
> >
> >    ---------------
> >
> >    $ head -n 4 info | tail -n 1
> >
> >    == Ouput
> >
> >          Host: localhost:8000
>
> $ grep "Host:" info
>
> >    $ head -n 4 info | tail -n 1 | cut -d ":" -f3
> >
> >
> >    == Ouput
> >            8000
>
> $ grep "Host:" info | cut -d ":" -f3
>
> Conor
> --
> Conor Daly
> Met Eireann, Glasnevin Hill, Dublin 9, Ireland
> Ph +353 1 8064276 Fax +353 1 8064247
> ------------------------------------
> bofh.irmet.ie running RedHat Linux  9:05am  up 2 days, 16:16,  2 users,
load average: 0.51, 0.43, 0.28
>
>
> **********************************************************************
> This email and any files transmitted with it are confidential and
> intended solely for the use of the individual or entity to whom they
> are addressed. If you have received this email in error please notify
> the system manager.
>
> This footnote also confirms that this email message has been swept
> for the presence of computer viruses.
>
>
> **********************************************************************
>
> _______________________________________________
> Courses mailing list
> Courses at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/courses




More information about the Courses mailing list