Need help with parser unix script

Ask a Question related to Linux / Unix Administration, Design and Development.

  1. #1

    Default Need help with parser unix script

    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
    yaniv Guest

  2. Similar Questions and Discussions

    1. #39098 [NEW]: parser bug: dash after if condition makes script crash without any error
      From: michael dot axt at mindcox dot com Operating system: Linux x86-64Bit PHP version: 5.1.6 PHP Bug Type: Scripting Engine...
    2. unix and Windows php script compatibility
      I'm a php novice and am developing a shopping cart application for a client who is hosted on a unix server. The hosting service requires that each...
    3. Simple Unix Shell Script
      Hi, I am trying to execute a simple shell script to set the environment variables. It runs fine without any errors but i doesn't sets the...
    4. question on a unix script
      Yes you are. cron is run with a different environment than exists in your shell. You must never assume that any command is in the path nor can...
    5. Unix alias command in script file
      Le 29/05/03 3:19, dans 0001HW.BAFAD84400017877F0305600@news.earthlink.net, « Dave Reiser » <dbreiser@earthlink.net> a écrit : Yet another option...
  3. #2

    Default Re: Need help with parser unix script

    yaniv 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.
    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
    Andy Hibbins Guest

  4. #3

    Default Re: Need help with parser unix script

    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.

    Dave Hinz Guest

  5. #4

    Default Re: Need help with parser unix script

    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
    yaniv Guest

  6. #5

    Default Re: Need help with parser unix script

    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 [url]http://cfaj.freeshell.org/shell[/url]
    ================================================== =================
    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
    Chris F.A. Johnson Guest

  7. #6

    Default Re: Need help with parser unix script

    Andy Hibbins wrote:
    > yaniv 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.
    >
    > 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.
    Rovall Guest

  8. #7

    Default Re: Need help with parser unix script

    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

    James T. Dennis Guest

  9. #8

    Default Re: Need help with parser unix script

    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
    John W. Krahn Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139