Unix administration - Need help with parser unix script

This is Interesting: Free IT Magazines  
Home > Archive > Unix administration > May 2004 > Need help with parser unix script





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Need help with parser unix script
yaniv

2004-05-16, 7:33 am

Hi all,

I am looking for a unix command/script that would return line i+1
according to String in line i.
I mean, if i found a string XXX in line 3 then return line 4 and so
on.

Any help would be apperciated,
-- Valiky
Dave Hinz

2004-05-16, 9:34 am

On 16 May 2004 04:28:52 -0700, yaniv <valiky@hotmail.com> wrote:
> Hi all,
>
> I am looking for a unix command/script that would return line i+1
> according to String in line i.
> I mean, if i found a string XXX in line 3 then return line 4 and so
> on.


When is your homework due? What have you tried so far? Show what
you've done & where you're stuck, or describe your approach...something.

yaniv

2004-05-16, 1:34 pm

It's not HW...

Basically i am looking to go over /etc/vfstab, which contains lines of
the following form:

# <Description>
/dev/dsk/... /home/bill ....

I want to print the device line in case i found a special string in
the description line right above.

So far I've tried:
1) sed
sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h
But i couldn't get rid of the description line in the output

2) grep -A1
but in case multiple lines matched, it returns the devices lines
seperated with --, which is quite annoying

help please...

tnx,
valiky
Chris F.A. Johnson

2004-05-16, 1:34 pm

On 2004-05-16, yaniv wrote:
> It's not HW...
>
> Basically i am looking to go over /etc/vfstab, which contains lines of
> the following form:
>
> # <Description>
> /dev/dsk/... /home/bill ....
>
> I want to print the device line in case i found a special string in
> the description line right above.
>
> So far I've tried:
> 1) sed
> sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h
> But i couldn't get rid of the description line in the output


sed -n '/regexp/{n;p;}'

> 2) grep -A1
> but in case multiple lines matched, it returns the devices lines
> seperated with --, which is quite annoying


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

2004-05-17, 4:36 pm

Andy Hibbins wrote:

> yaniv wrote:
>
>
> The gnu grep command may be what you need:
>
> grep -A 1 XXX /etc/afilename | grep -v XXX
>
> where XXX is the string to search
> and /etc/afilename is the filename to search.
>
> say if /etc/afilename contained the following:
>
> XXXdf
> 1
> XXXfe
> 2
> XXXge
> 3
> XXXht
> 4
>
> Output of the command would be:
>
> 1
> 2
> 3
> 4


Not exactly. If you have a text like:

# bill's homedir
/dev/dsk/... /home/bill ....

and then do

grep -A 1 bill /etc/afilename | grep -v bill

you'll have null output!

For this case (searching through comments), I'd suppose to use something
like this:

grep -A 1 XXX <file> | grep -v '^#' | grep -v '^--$'

Second grep to filter out comments, third to avoid -- lines.
James T. Dennis

2004-05-18, 1:36 am

yaniv <valiky@hotmail.com> wrote:
> Hi all,


> I am looking for a unix command/script that would return line i+1
> according to String in line i.
> I mean, if i found a string XXX in line 3 then return line 4 and so
> on.


> Any help would be apperciated,
> -- Valiky


#!/bin/sh
while read line; do
case "$line" in
*XXX*) read line; echo "$line";;
esac
done

Just call that in a pipeline or with input redirection. This one just
uses shell built-ins and should work for any Bourne compatible shell. It
shouldn't need any bash or Korn extensions.




--
Jim Dennis,
Starshine: Signed, Sealed, Delivered

John W. Krahn

2004-05-18, 3:34 am

yaniv wrote:
>
> I am looking for a unix command/script that would return line i+1
> according to String in line i.
> I mean, if i found a string XXX in line 3 then return line 4 and so
> on.


perl -ne'/XXX/&&print scalar<>' yourfile


John
--
use Perl;
program
fulfillment
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com