Results 1 to 4 of 4

Thread: Grep Terminal Command Help

  1. #1
    Join Date
    Jun 2013
    Beans
    1

    Grep Terminal Command Help

    Hey this will be my first post to Ubuntu forms so i hope i am in the right place anyway here i go.

    I am wanting to use the "grep" command to search and return the amount of times "G" occurs in the .txt file. (example below)

    Name|Name_2|Address_1|Address_2|Address_3|Postcode |Phone_number

    what i am trying to do is just search the column where all the postcodes are in (eg column 6).


    grep 'G' filename.txt -c

    i understand that the command above will display every single "G" in the text file which is useless. What i am looking for is something like what is below

    grep 'G' column6 separated by | filename.txt -c




    would really appreciate if someone could help.

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

    Re: Grep Terminal Command Help

    Hello and welcome to the forum

    Are the entries literally formatted as shown in your example (i.e. exactly 5 fields ahead of the postal code, separated with pipe (|) characters)? if so you could do something with grep's extended regular expression (-E) mode e.g.

    Code:
    grep -E '([^|]*|){5}([^|]*G[^|]*)' yourfile
    Alternatively, you could look at other tools like 'awk' which are often better for 'structured' input

    Code:
    awk -F"|" '$6 ~ /G/ {print}' yourfile
    For the count using awk, the easiest way imho is just to pipe the output to the 'wc' program

    Code:
    awk -F"|" '$6 ~ /G/ {print}' yourfile | wc -l
    however you *could* do the counting in awk if you wanted to

    Code:
    awk -F"|" 'BEGIN{n=0}; $6 ~ /G/ {n=n+1}; END {print n}' yourfile
    Last edited by steeldriver; June 5th, 2013 at 03:51 PM.

  3. #3
    Join Date
    Jan 2009
    Location
    ::1
    Beans
    2,485

    Re: Grep Terminal Command Help

    What I always do, is this: step by step, build a one-liner with filters in it.

    My example input is in blabla.txt. See the end for content.

    First print the file

    Code:
    cat blabla.txt
    Then use | as the seperator and get element 6 from each line:

    Code:
    cat blabla.txt | awk -F\| '{ print $6 }'
    Then find the letter G, and ignore case:

    Code:
    cat blabla.txt | awk -F\| '{ print $6 }' | grep -i G
    Then count the occurences:

    Code:
    cat blabla.txt | awk -F\| '{ print $6 }' | grep -i G | wc -l
    Result:

    Code:
    sander@hapee:~$ cat blabla.txt | awk -F\| '{ print $6 }' | grep -i G | wc -l
    2
    sander@hapee:~$
    Ah, that's correct!

    blabla.txt:

    Code:
    Name|Name_2|Address_1|Address_2|Address_3|Postcode |Phone_number
    Name|Name_2|Address_1|Address_2|Address_3|12345G|Phone_number
    Name|Name_2|Address_1|Address_2|Address_3|Eg4444 |Phone_number
    Name|Name_2|Address_1|Address_2|Address_3|abCDF |Phone_number

  4. #4
    Join Date
    Nov 2008
    Location
    S.H.I.E.L.D. 6-1-6
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: Grep Terminal Command Help

    moved to absolute beginners section
    Don't waste your energy trying to change opinions ... Do your thing, and don't care if they like it.

Tags for this Thread

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
  •