sed question
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix questions > sed question




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    sed question  
usatim


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-16-04 02:28 AM

how can I find a string in a file, print that line and the next two lines?





[ Post a follow-up to this message ]



    Re: sed question  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-16-04 02:28 AM

On 2004-10-13, usatim wrote:
> how can I find a string in a file, print that line and the next two lines?

Why sed?

If you have GNU grep:

grep -A2 "$string" file


With awk:

awk "/$string/ {print; getline; print; getline; print; exit}"


With the shell:

while IFS= read -r line
do
case $line in
*"$string"*) printf "%s\n" "$line"
IFS= read -r line
printf "%s\n" "$line"
IFS= read -r line
printf "%s\n" "$line"
exit
;;
esac
done < file


And if you really want to use sed:

sed -n "/$string/{p;n;p;n;p;q;}" file


--
Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
 ========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License





[ Post a follow-up to this message ]



    Re: sed question  
usatim


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-16-04 02:28 AM

"Chris F.A. Johnson" <cfajohnson@gmail.com> wrote in message news:<2t5lobF1smmp7U1@uni-berli
n.de>...
> On 2004-10-13, usatim wrote: 
>
>    Why sed?
>
>    If you have GNU grep:
>
> grep -A2 "$string" file
>
>
>    With awk:
>
> awk "/$string/ {print; getline; print; getline; print; exit}"
>
>
>    With the shell:
>
> while IFS= read -r line
> do
>   case $line in
>       *"$string"*) printf "%s\n" "$line"
>                    IFS= read -r line
>                    printf "%s\n" "$line"
>                    IFS= read -r line
>                    printf "%s\n" "$line"
>                    exit
>                    ;;
>   esac
> done < file
>
>
>    And if you really want to use sed:
>
> sed -n "/$string/{p;n;p;n;p;q;}" file

Thanks for the response. I was close but I did not have the brackets
and I did not have the last ;.

I know how to find a string and delete it with sed but I have no clue
about how to find string1 and if the next line is string2, delete both
lines, throughout a file. This should be the last time I bother
everyone.

Is there a good sed book I can get. I am not happy with the one I have
(O'Reilly, sed & awk).





[ Post a follow-up to this message ]



    Re: sed question  
rakesh sharma


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-16-04 02:28 AM

tillman.h.craft@usa-spaceops.com (usatim) wrote in message news:

>
> I know how to find a string and delete it with sed but I have no clue
> about how to find string1 and if the next line is string2, delete both
> lines, throughout a file. This should be the last time I bother
> everyone.
>

sed -e '
/string1/!b
$!N
/\nstring2/d
' yourfile

>
> Is there a good sed book I can get. I am not happy with the one I have
> (O'Reilly, sed & awk).
>

That's the only book or look for the sed one-liners web site where
there are good tutorials on 'sed'.





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:34 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register