#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    76
    Rep Power
    12

    how to match line that contian


    I have two variable that contain
    $1 = unix
    $2 = bjc
    now I want to match any lines in my file that contain those variable, the lines is separated by comma

    Example
    window,bjc 210,computer
    unix, bjc 210, computer

    from the two line example above I want to print
    unix, bjc 210, computer
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    137
    Rep Power
    0
    Grep (and/or egrep) are the tools to use. Example:

    Code:
    #!/bin/bash
    INFILE="infile"
    PLATFORM="unix"
    TYPE="bjc"
    grep $PLATFORM $INFILE | grep $TYPE
    The last line does what you want. The first part looks for 'unix' ($PLATFORM) in every line of input from 'infile' ($INFILE). If a hit is found, the second part checks to see if that line also contains 'bjc' ($TYPE). If this is the case the line is printed, if not, grep checks the next line of input.

    One tip: Do not use $1, $2 etc to store variables. These are special and hold information about the program and parameters. I used 'human readable' names for the variables (PLATFORM and TYPE).

    Hope this gets you going again.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    76
    Rep Power
    12
    Originally Posted by druuna
    Grep (and/or egrep) are the tools to use. Example:

    Code:
    #!/bin/bash
    INFILE="infile"
    PLATFORM="unix"
    TYPE="bjc"
    grep $PLATFORM $INFILE | grep $TYPE
    The last line does what you want. The first part looks for 'unix' ($PLATFORM) in every line of input from 'infile' ($INFILE). If a hit is found, the second part checks to see if that line also contains 'bjc' ($TYPE). If this is the case the line is printed, if not, grep checks the next line of input.

    One tip: Do not use $1, $2 etc to store variables. These are special and hold information about the program and parameters. I used 'human readable' names for the variables (PLATFORM and TYPE).

    Hope this gets you going again.
    Thanx

IMN logo majestic logo threadwatch logo seochat tools logo