Results 1 to 6 of 6

Thread: Using Grep in bash

  1. #1
    Join Date
    Nov 2012
    Beans
    23

    Using Grep in bash

    Hi,

    I have two files (ASCII text), pass.old and pass.new, I want to find all the lines in pass.new which don't appear in pass.old (most lines overlap)... here's what I've got so far:

    sort -n pass.old pass.new | uniq -u

    that prints the lines in pass.new which don't appear in pass.old (but also the ones in pass.old which don't appear in pass.new), I want the output to be only the ones which appear in pass.new and not pass.old, I recon it'll be something to do with grep.

    I've tried a few things like:

    grep (sort -n pass.old pass.new | uniq -u) pass.new
    or grep [sort -n pass.old pass.new | uniq -u] pass.new

    I always get error messages, and I'm not quite sure how to write it so that it does grep of the output of sort -n etc. on pass.new,

    Hopefully that made sense,

    Any help/ideas on this one?

  2. #2
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Using Grep in bash

    why not use use diff?

    Code:
    diff pass.new pass.old
    Last edited by steeldriver; November 18th, 2012 at 04:37 PM.

  3. #3
    Join Date
    Sep 2009
    Location
    Freiburg/Germany
    Beans
    1,112
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: Using Grep in bash

    Code:
     grep -v -F -f pass.old pass.new
    ClassicMenu Indicator - classic GNOME menu for Unity
    Unsettings - configuration program for the Unity
    Privacy Indicator - easily switch privacy settings in Unity
    Arronax - create and modify app starters

  4. #4
    Join Date
    Nov 2012
    Beans
    23

    Re: Using Grep in bash

    Quote Originally Posted by steeldriver View Post
    why not use use diff?

    Code:
    diff pass.new pass.old
    Two reasons, firstly the lines have to be in the same order for diff to print the lines in 1 which aren't in the other, (although in this case they were, but I'm also interested in a more general method for doing it incase I come across similar problems in the future).

    Also diff prints the different output from both files (although to be fair it does specify which file each line came from),

    Not a bad shout, though, and cheers for the input.

  5. #5
    Join Date
    Nov 2012
    Beans
    23

    Re: Using Grep in bash

    Quote Originally Posted by diesch View Post
    Code:
     grep -v -F -f pass.old pass.new
    Worked nicely. Cheers.

    Also, as a side thing, is it possible to do what I was trying to do earlier somehow?

    i.e. do: grep [output of: sort -n pass.new pass.old | uniq -u] pass.new

    (so it prints the matches of each outputted line from: sort -n pass.new pass.old | uniq -u with file pass.new)?

  6. #6
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Using Grep in bash

    if you want to embed a command you need $() not ()

    this should sorta work
    Code:
    grep -E "^("$( sort pass.* | uniq -u | tr '\n' '|' )"$)$" pass.new
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

Bookmarks

Posting Permissions

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