Find the answer to your Linux question:
Results 1 to 8 of 8
Hi everyone, im trying to make the following command line shorter by introducing a script that join up all the grep commands ./new1a < numbers.txt | grep -i -v '^a ...
  1. #1
    Just Joined!
    Join Date
    Jan 2008
    Posts
    4

    grep/awk help, how do i make this shorter?

    Hi everyone, im trying to make the following command line shorter by introducing a script that join up all the grep commands

    ./new1a < numbers.txt | grep -i -v '^a ' | grep -i -v '^the ' | grep -i -v '^or ' | sort -f

    How would I go about merging all the greps into a scripe and putting all the words that should be excluded into a data file

    or i could use the awk command that franklin suggested, something like

    awk '
    while(stdin == FILE > 0 ) {
    stdin[$1] = $1
    }
    }
    !stdin[$1]{print}
    ' "datafile.txt"

    edit: nvm franklin got it the same way

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    show how your numbers.txt look like, and also describe how you want to final output to be

  3. #3
    Just Joined!
    Join Date
    Jan 2008
    Posts
    4
    numbers.txt have words not numbers sorry


    like
    ---numbers.txt---
    hi how are you
    what are you doing
    or am i sleeping
    the zebra ate the lion
    yes sir
    a banana fell
    ------------------


    So I Would want to remove the lines that start with or, the, a so in the above file, line 3,4,and 6 would be removed. Thanks

    The rest of the file should remain unchanged

  4. #4
    Just Joined!
    Join Date
    Jan 2008
    Posts
    1
    first thing I would try about making this shorter was to only call grep once, and then use subexpression.
    That is the easiest way to start something like
    grep (-i -v ^the| -i -v ^or| -i -v ^a) instead of calling grep multiply times, and beyond this I cannot help - I know not so much myself yet....
    But that is the more obvious way to go

  5. #5
    Just Joined!
    Join Date
    Jan 2008
    Posts
    4
    alright i made it shorter, and this works, but what do i do if i have a txt file that has more words i want to exclude? like -- and, if, you, etc etc

    ./new < numbers.txt | grep -i -v '^a \|^the \|^or ' | sort -f

    looks like grep is not so good to make it for a list, so im going to work with the awk, seeing you can do actual programming in it

  6. #6
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    See the solution below.

    Regards

  7. #7
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    The OP deleted his question, for clarity, this was the request:

    Output of his command "./new < numbers.txt ":

    hi how are you
    what are you doing
    or am i sleeping
    the zebra ate the lion
    yes sir
    a banana fell

    File datafile.txt:

    a
    the
    or

    Desired output:

    hi how are you
    what are you doing
    yes sir
    Try this:

    Code:
    ./new < numbers.txt | awk '
      BEGIN{while(getline < "datafile.txt" > 0 ) {
        arr[$1]=$1
      }
      close("datafile.txt")
    }
    !arr[$1]{print}
    '
    Regards

  8. #8
    Just Joined!
    Join Date
    Jan 2008
    Posts
    4
    cool, thanks frank i actually spend like 5 hours and finally got it working, and i check back here and you have almost same thing but better..what a waste of time lool, but i did learn alot

    thanks

Posting Permissions

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