Re: Bash/grep -e filtering quickie question

From: The Loeki (the.loeki_at_gmail.com)
Date: 05/02/05

  • Next message: Martijn Lievaart: "Re: CRON : Can you send values to CRON and get a result ?"
    Date: 2 May 2005 02:02:52 -0700
    
    

    Logan Shaw wrote:
    > The Loeki wrote:
    > > Say I've got this:
    > > foo;bar;12;foobar

    > I assume by "got this" you mean that you've got it in a file. As
    > opposed to as input to the shell, which it would be syntactically
    > valid for!

    > > The 12 are a variable number of figures.

    > OK, so the third field in your file is a string of digits.

    Sorry 'bout that. I indeed have 2 lists, and I wish to parse one field
    of one list with all the digits in one field of the other, and output
    it to file no.3
    Or, to put it into perspective:
    I've got a list of people who don't work on certain days (e.g.MOndays
    and Tuesdays, 1 and 2) and I wish to parse my generated list of dates
    with it.

    > > I want another list to be filtered with all the numbers
    individually,
    > > as in grep -e"$(echo "^1" ; echo "^2")" (or something).

    > The first task is to extract that field from the file. That will
    > look something like this:

    > linewithdigits=`cat file` # or something else if it's
    multiline
    > digitstr=`echo $linewithdigits | cut -d';' -f3`

    I'll have to look that cut command up, it looks cuhl (I've been
    massively awk-ing)

    > Now you need to split digitstr up into individual digits. I don't
    > know of a really clean way to do this, but one possible way is to
    > use sed to insert spaces, then allow the shell to split it up into
    > separate digits:

    > for digit in `echo "$digitstr" | sed -e 's/\(.\)/\1 /g'`
    > do
    > grep "^$digit" whatever
    > done

    the sed command was about what I was looking for. I'll be testing it
    and if it looks good enough, I'll use it!
    For now, I've solved it like this:

    partt=$(echo $i | awk -F";" '{ print $2 }')
    if [[ ! -z $partt ]]
    then if [[ ${#partt} == 1 ]]
         then grep -v "^$partt," /tmp/list1 > /tmp/d$iteration
         else /usr/xpg4/bin/grep -v -e"$(IFS=' ' ; for wd in $(echo $i |
    awk -F";" '{ print $2 }') ; do echo "^$wd," ; done)" /tmp/list1 >
    /tmp/d$iteration
         fi
    else cp /tmp/list1 /tmp/d$iteration
    fi

    This works well, however, due to the IFS (which, IMHO, is one of thé
    single cuhlest variables in the book) variable, only in Solaris. And as

    you can see by the IFS=' ', I've had to manually adapt the field spec
    to be in the order of
    foo;bar;1 2;foobar

    > However, your message is a little unclear about what you want to do.
    > I'm not sure if you want to do a separate grep for each digit or
    > you want to do a single grep for all the digits at once. If you
    > want to do a single grep for all the lines that, say, begin with
    > "1", "2", or "3", you can just transform the digit string into a
    > grep pattern and avoid the loop and the splitting and all that:

    > digitstr=`echo $linewithdigits | cut -d';' -f3`
    > pattern="^[$digitstr]"
    > grep "$pattern" whatever

    That's about right

    Ed Morton wrote:
    > awk -F\; 'NF==FNR{m=m s $3;s="|"}$0 ~ m' file1 file2
    >ITIM:
    >awk -F\; 'NF==FNR{m=m s $3;s="|";next}$0 ~ m' file1 file2

    I can't get it to filter. It just passes everything through.


  • Next message: Martijn Lievaart: "Re: CRON : Can you send values to CRON and get a result ?"

    Relevant Pages

    • Re: bash scripting question
      ... so the third field in your file is a string of digits. ... I indeed have 2 lists, and I wish to parse one field ... > use sed to insert spaces, then allow the shell to split it up into ... > you want to do a single grep for all the digits at once. ...
      (comp.unix.solaris)
    • Re: MC: 4th power with no small digits
      ... I just bought my first HP calculator a few months ago. ... website now lists the model I have as current and doesn't mention a ... would be nice to separate the number out into a list of digits. ... The next routine takes a list and a number, ...
      (comp.sys.hp48)
    • Re: An uncountable countable set
      ... You intermingle numbers with digits. ... What is the tenth path in your tree? ... it holds for *all* lists. ... If all edges terminate, also all paths terminate, and both are ...
      (sci.math)
    • Re: abundance of irrationals!)
      ... contains only finitely many digits. ... That is only true for physical lists, ... If you mean that the decimal representation of 1/3 has MORE digits than ...
      (sci.math)
    • Re: How to prove an infinite set bigger than N
      ... consecutive digits of OL ... This has been explained to you dozens of times with dozens of different ... lists are also represented ... C-- a representation of the ...
      (sci.math)