RE: Summary Grep, Sed, Awk - From and to for those non "fluent"

From: Ragnar Moller \(MA/EAF\) <ragnar.moller_at_ericsson.com>
Date: Fri Apr 07 2006 - 10:21:34 EDT
> I got a lot of answers. I had a job to do so I triggered on and tried
> the first one which almost evolved the "unproved" by Stephen.Grund
> (but step by step).
> I think they're really worth sharing for those of us who aren't yet
> "fluent" in Unix.
> Like I said I only tried sed, but I certainly will take the time to
> try out the other ones because I'll be needing a higher performing and
> table solution next round.
>
> Here are the answers:
> Use sed  by majority
> Cat file | -n '/cecilia/,/frederic/ p'
> Use Awk   25%
> awk '/from/,/to/' filename
> awk '/cecilia/,/frederic/ { print }'
> Unproved by Stephen Grund
> set from=`grep -n fromfirst filename | head -1 | awk -F: '{print $1}'`
> set last=`grep -n tolast filename | tail -1 | awk -F: '{print $1}'`
> awk '(NR>=from)&&(NR<=last)' filename
> This is exactly what perl was developed for. Pretty simple to do.
> Kristopher Briscoe
> http://freshmeat.net/projects/cgrep  James Noyes
> cgrep provides many of the features of grep, egrep, and fgrep with
> enhanced performance along with many additional features. One of the
> most important features is the ability to output the context
> (surrounding lines) of the matching lines. The context window may be
> specified as a constant number of lines before and after the matching
> lines, by specifying beginning and ending delimiters, or various
> combinations thereof.
>
> Use a shell script  Dwight Brewer
>
> #!/usr/bin/sh
>
> inbetween=false
> echo "" > newfile
>
> cat data |while read line
> do
>    start_check=`echo $line | grep cecilia`
>    if [ "$start_check" ]
>    then
>         ## we found start line containing cecilia
>         inbetween=true
>     fi
>
>     if [ "$inbetween" = "true" ]
>     then
>         echo $line >> newfile
>         end_check=`echo $line | grep frederic`
>         if [ "$end_check" ]
>         then
>            ## we found end line containing frederic
>            inbetween=false
>         fi
>      fi
> done
>
>
> Use Perl by Ric Anderson
>
> Depending on how much you need to do, awk or perl will handle this
> task.  Something like
>   awk -f awkscript < your_file
> where awkscript contains
> { if($0 ~ /cecilia/) {
>     print $0;flag=1;
>   }
>   else {
>     if(flag == 1) {
>       print $0;
>       if($0 ~ /frederic/) {
>         exit(0);
>       }
>     }
>   }
> }
> will print all the information beginning with a line containing
> cecilia and ending with a line containing frederic.  Note the match is
> case sensitive.
> Minor edits to the logic here would allow suppressing the cecilia and
> frederick lines and only displaying the information between those
> points.
>
> Perl provides more powerful pattern matching, and a lot of other
> useful features that make it a better choice than awk for more complex
> tasks.
_______________________________________________
sunmanagers mailing list
sunmanagers@sunmanagers.org
http://www.sunmanagers.org/mailman/listinfo/sunmanagers
Received on Fri Apr 7 11:06:57 2006

This archive was generated by hypermail 2.1.8 : Thu Mar 03 2016 - 06:43:57 EST