Results 1 to 5 of 5

Thread: how to export first string in command

  1. #1
    Join Date
    Jun 2011
    Beans
    3

    how to export first string in command

    Hi, please help me in the following matter, that I would like to export the first string of certain line of command, for example:

    root@ubuntu:~# who
    root pts/0 2012-12-25 17:18 (ubuntu.domain.name.com)
    firstname.lastname tty7 2012-12-25 16:55

    The thing that I need is ONLY "firstname.lastname" information, not including the rest.
    Much appreciated!
    Have a good day guys.

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

    Re: how to export first string in command

    a couple of ideas

    Code:
    $ echo "firstname.lastname tty7 2012-12-25 16:55" | awk '{print $1}'
    firstname.lastname
    $
    $ echo "firstname.lastname tty7 2012-12-25 16:55" | cut -d' ' -f1
    firstname.lastname
    $

  3. #3
    Join Date
    Dec 2009
    Location
    germany
    Beans
    1,020
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: how to export first string in command

    Quote Originally Posted by sccuser View Post
    Hi, please help me in the following matter, that I would like to export the first string of certain line of command, for example:

    root@ubuntu:~# who
    root pts/0 2012-12-25 17:18 (ubuntu.domain.name.com)
    firstname.lastname tty7 2012-12-25 16:55

    The thing that I need is ONLY "firstname.lastname" information, not including the rest.
    Much appreciated!
    Have a good day guys.
    hi
    who | awk '{print $1}'
    "What is the robbing of a bank compared to the FOUNDING of a bank?" Berthold Brecht

  4. #4
    Join Date
    Jun 2011
    Beans
    3

    [SOLVED] how to export first string in command

    Perfect!
    Thanks so much steeldriver & rnerwein.

    Just one more question please.
    For the out put
    firstname.lastname tty7 2012-12-25 16:55
    what's the solution if I need
    tty7
    or
    2012-12-25
    or
    16:55
    Edit: yep I've got it, please forget. Thanks guys!
    Last edited by sccuser; December 25th, 2012 at 03:09 PM.

  5. #5
    Join Date
    Dec 2009
    Location
    germany
    Beans
    1,020
    Distro
    Ubuntu 12.04 Precise Pangolin

    Wink Re: [SOLVED] how to export first string in command

    Quote Originally Posted by sccuser View Post
    Perfect!
    Thanks so much steeldriver & rnerwein.

    Just one more question please.
    For the out put
    what's the solution if I need
    or
    or


    Edit: yep I've got it, please forget. Thanks guys!
    ok
    $0 is the whole string (line)
    $1 is the first field delimited by " " space ....
    $2 is the second field
    ......
    so if you want to have only the time: who | awk ' { print $4 }'
    and if you want to get the tty : who | awk '/tty/ { print $2}'
    awk is a powerful tool try to learn it.
    cheers
    "What is the robbing of a bank compared to the FOUNDING of a bank?" Berthold Brecht

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
  •