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

    Join Date
    Jul 2013
    Posts
    23
    Rep Power
    0

    O eliminate a string pattern from a file


    am having a file where i need to take eliminate the a string pattern from input file.

    for eg
    list.txt
    Code:
    AUS
    USA
    
    input.txt
    AUS,123
    NZ,11
    US,13
    USA,12,
    USA,12,AUS
    AUSAA,1,2
    i need to eliminate the string from list.txt which matches the first string from input file
    
    desired output
    
    NZ,11
    US,13
    AUSAA,1,2
    i tried by using the below code
    
    for i in list.txt
    do
    grep -v ^$i input.txt>out.txt
    done
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,367
    Rep Power
    1870
    > grep -v ^$i input.txt>out.txt
    So when you've done one elimination, you need to rename out.txt to input.txt before going round the loop again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    23
    Rep Power
    0
    Originally Posted by salem
    > grep -v ^$i input.txt>out.txt
    So when you've done one elimination, you need to rename out.txt to input.txt before going round the loop again.
    i don't want to loop again and again rather to map the pattern directly is there any other way
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,367
    Rep Power
    1870
    If you didn't want a loop, why did you write a loop?

    If list.txt is reasonably short, you could turn it into the pattern
    egrep -v '^(AUS|USA)' input.txt > output.txt
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    23
    Rep Power
    0
    Originally Posted by salem
    If you didn't want a loop, why did you write a loop?

    If list.txt is reasonably short, you could turn it into the pattern
    egrep -v '^(AUS|USA)' input.txt > output.txt
    i tried with the below code can you help on this if possible


    $ sed 's/.*/^&,/' list>list.tmp ; grep -vEf list.tmp input ; rm -f list.tmp
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,400
    Rep Power
    1688
    With that last example whilst the commands may be working exactly as desired you are not redirecting the output of the grep anywhere, so it's going to stdout and not saved. I'd also suspect that you amy be losing AUSSA with the inverse grep of AUS unless you, as shown by Salem, append a space character.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    23
    Rep Power
    0
    Originally Posted by SimonJM
    With that last example whilst the commands may be working exactly as desired you are not redirecting the output of the grep anywhere, so it's going to stdout and not saved. I'd also suspect that you amy be losing AUSSA with the inverse grep of AUS unless you, as shown by Salem, append a space character.
    hi simon
    can you tell me the code
  14. #8
  15. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,367
    Rep Power
    1870
    Since you seem to have discovered the -f option of grep, is there in fact anything that you need to do to prepare the search terms?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,400
    Rep Power
    1688
    Originally Posted by vasanth_amrita
    hi simon
    can you tell me the code
    It'd be something akin to:

    Code:
    sed 's/.*/^&,/' list>list.tmp ; grep -vEf list.tmp input > out.txt ; rm -f list.tmp
    Basically what you had in place originally to save the output
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo