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

    Join Date
    Sep 2006
    Posts
    2
    Rep Power
    0

    Geeting only required Data


    I have a file:
    gk@dev1:~> cat more.txt
    iam :here home/usr/bin
    iam :here home/usr
    iam :here help/home/bin
    iam :here home/bin/var

    i want to cut the some of the letters from the above file and print only the last letters like
    my data would be
    bin
    usr
    bin
    var

    How do i do that?

    any one ..need help?
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Location
    Dublin, Ireland
    Posts
    174
    Rep Power
    49
    The easiest way is probably to replace all of the / with white space then print the last column.

    Code:
    sed -e 's/\// /g' test.txt | awk '{print $NF}'
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2006
    Location
    Sweden
    Posts
    14
    Rep Power
    0
    No need to change the delimiter, just specify it:

    Code:
    awk '{FS="/";print $NF}' file
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Location
    Dublin, Ireland
    Posts
    174
    Rep Power
    49
    Originally Posted by Verletto
    No need to change the delimiter, just specify it:

    Code:
    awk '{FS="/";print $NF}' file
    I decided against that, because the file does not necessarily have to contain a /.

    i.e.

    Code:
    iam :here home/usr/bin
    iam :here help/home/bin
    iam :here home
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2006
    Posts
    177
    Rep Power
    238
    Originally Posted by gkrishnag
    I have a file:
    gk@dev1:~> cat more.txt
    iam :here home/usr/bin
    iam :here home/usr
    iam :here help/home/bin
    iam :here home/bin/var

    i want to cut the some of the letters from the above file and print only the last letters like
    my data would be
    bin
    usr
    bin
    var

    How do i do that?

    any one ..need help?
    Parsing of strings/text is very easy, in Python.
    Code:
    >>> for lines in open("more.txt"):
    ... 	os.path.split(lines.split()[-1])[-1]
    ... 
    'bin'
    'bin'
    'home'
  10. #6
  11. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,688
    Rep Power
    1841
    Very interesting, but not the output I woudl have expected - should it not have returned:

    Code:
    bin
    usr
    bin
    var
    from that input file?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2006
    Posts
    177
    Rep Power
    238
    Originally Posted by SimonJM
    Very interesting, but not the output I woudl have expected - should it not have returned:

    Code:
    bin
    usr
    bin
    var
    from that input file?
    I had a different input file for testing, and pasted the wrong output.

    Code:
    >>> for lines in open("more.txt"):
    ... 	print os.path.split(lines.split()[-1])[-1]
    ... 
    bin
    usr
    bin
    var

IMN logo majestic logo threadwatch logo seochat tools logo