Find the answer to your Linux question:
Results 1 to 7 of 7

Thread: how to grep

Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    how to grep


    Hi please help me. It is output of my file.
    i just want to grep column "Username"
    and save it to another file.
    can it be implemented using grep? if yes then how?
    if no then how to do this?
    thanx in advance

    SessionID Line/Chan Slot:Item Address Username

    416150196 1.02.06/006 1:04:11/000 140 riazali

    416150328 1.02.04/026 1:05:26/000 122 wbsssygx

    416150351 1.02.08/029 1:05:28/000 138 aeqthucw

    416150444 1.02.08/010 1:03:03/000 221 alsaaim

    416150355 1.02.05/010 1:05:32/000 121 ve9us

    416150394 1.02.07/009 1:04:35/000 111 mahad

  2. #2
    If I understand correctly what you are wanting to do, try this:

    Code:
    grep "NAME$" INPUT_FILE > OUTPUT_FILE
    example: grep "riazali$" MyDataFile > MyResultsFile

    The '$' matches end-of-line.

    If you are using a variable, containing the name, then:

    Code:
    grep "$NAME$" INPUT_FILE > OUTPUT_FILE
    where: '$NAME' is the variable name itself:

    Code:
    NAME="riazali"
    grep "$NAME$" INPUT_FILE > OUTPUT_FILE

  3. #3
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    Also give this a try:
    Code:
    grep -o '[A-Za-z0-9]*$' data-file > out-file
    This regexp makes a couple assumption (namely about your data, and what you've shown us here).

    Personally, I'd do this quick and easy with cut.

    Code:
    cat data-file | cut -f 5 -d ' ' > out-file
    Again, this makes some assumptions (i.e. that your delimiter is a blank space and that I got the field number right).

  4. $spacer_open
    $spacer_close
  5. #4
    Quote Originally Posted by anomie
    Also give this a try:
    Code:
    grep -o '[A-Za-z0-9]*$' data-file > out-file
    This regexp makes a couple assumption (namely about your data, and what you've shown us here).

    Personally, I'd do this quick and easy with cut.

    Code:
    cat data-file | cut -f 5 -d ' ' > out-file
    Again, this makes some assumptions (i.e. that your delimiter is a blank space and that I got the field number right).
    Your solutions give different results: the first give the whole matching line, the second, which I think is what the OP actually wants, gives the list of usernames. BTW, why cat the file into cut when cut takes a file argument - you're a bit of a process hog!

    Alternatively, to get the last field of each line (regardless of the number of fields):
    Code:
    awk '{printf $NF]' data-file

  6. #5
    Quote Originally Posted by scm
    Your solutions give different results: the first give the whole matching line, the second, which I think is what the OP actually wants, gives the list of usernames. BTW, why cat the file into cut when cut takes a file argument - you're a bit of a process hog!

    Alternatively, to get the last field of each line (regardless of the number of fields):
    Code:
    awk '{printf $NF]' data-file
    Sorry the ']' should be a '}' (I can't edit the post, because it crashes my browser).

  7. #6
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    Well, both bits I posted give identical results to me.

    Contents of data-file (I copied directly from his post and then removed the blank lines):
    Code:
    SessionID Line/Chan Slot:Item Address Username
    416150196 1.02.06/006 1:04:11/000 140 riazali
    416150328 1.02.04/026 1:05:26/000 122 wbsssygx
    416150351 1.02.08/029 1:05:28/000 138 aeqthucw
    416150444 1.02.08/010 1:03:03/000 221 alsaaim
    416150355 1.02.05/010 1:05:32/000 121 ve9us
    416150394 1.02.07/009 1:04:35/000 111 mahad
    Command:
    Code:
    grep -o '[A-Za-z0-9]*$' data-file > out-file
    Results (out-file):
    Code:
    Username
    riazali
    wbsssygx
    aeqthucw
    alsaaim
    ve9us
    mahad
    Command:
    Code:
    cat data-file | cut -f 5 -d ' ' > out-file
    Results (out-file):
    Code:
    Username
    riazali
    wbsssygx
    aeqthucw
    alsaaim
    ve9us
    mahad
    I see no difference.

    My system:
    • FreeBSD 5.4-RELEASE
    • grep (GNU grep) 2.5.1-FreeBSD
    • GNU bash, version 3.00.16(1)-release

  8. #7
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    P.S. I just ran the exact same commands on my test box:
    • SuSE 9.1 Personal, Linux 2.6.15.1
    • grep (GNU grep) 2.5.1
    • GNU bash, version 2.05b.0(1)-release


    And I got the exact same results.

    You probably didn't copy my commands correctly. Did you forget the -o switch for grep, maybe?

Posting Permissions

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