Find the answer to your Linux question:
Results 1 to 5 of 5
Hi All, I am trying to extract the values of Student Id information from the file which 1000 of lines of information as below Student Id,10261.15147,Course,157 Student Id,102.10147,Course,15 Output should ...
  1. #1
    Just Joined!
    Join Date
    Jun 2007
    Posts
    4

    Help with Grep

    Hi All,

    I am trying to extract the values of Student Id information from the file which 1000 of lines of information as below

    Student Id,10261.15147,Course,157
    Student Id,102.10147,Course,15

    Output should be
    10261.15147
    102.10147

    I am struggling to do this. As this is my first exp. in doing Shell script.

    I did an grep -o ',[0-9]*.[0-9]*,' filename

    i was able to get the following output

    ,10261.15147,
    ,102.10147,

    But was not able to remove the , in start and end of each line. Can anyone help me to do this.

    Thanks in Advance.

    Regards,
    Arul.

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Code:
    awk -F"," '{print $2}' file

  3. #3
    Just Joined!
    Join Date
    Jun 2007
    Posts
    4
    Thanks a lot.

    Regards,
    Arul.

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Or:

    Code:
    cut -d"," -f2 file
    Regards

  5. #5
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    For posterity's sake, here is a grep way to do it.

    Code:
    [hal@troy ~]$ cat some-file 
    Student Id,10261.15147,Course,157
    Student Id,102.10147,Course,15
    
    [hal@troy ~]$ grep -o '[0-9]\{1,\}\.[0-9]\{1,\}' some-file 
    10261.15147
    102.10147
    It assumes that your data contains one or more numerics, followed by a dot (.), followed by one or more numerics. You have to know your data in order for this to work correctly.

    The -o option only selects the matching expression as you probably know.

Posting Permissions

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