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

    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Someone save me!!


    Below is a batchfile and the output im getting. What I wanna do is create a header that says "User TTY Date and Time" all evenly spaced over their appropriate columns from the who command. As you can see its not working out. Does anyone have a minute to give me some suggestions?

    echo User TTY Date Time
    who | cut -c1-7,9-15,23-25,28-34

    User TTY Date Time
    aar11 pts/2 Feb4 12:27
    b11212d pts/1 Feb4 13:56
    alf112 pts/6 Feb4 13:33
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,451
    Rep Power
    1751
    First off you are making some assumptions about the format of the output from the who command, it produces different output on my little Linux netbook, for example.

    I'd be more inclined to lean toward using either tabs in your output (\t) and to use a field-based parser (cut -d or awk) for parsing the who output. With acknowledged differences with the who command:
    Code:
    $ who
    ehb      tty7         2011-02-06 12:28 (:0)
    ehb      pts/0        2011-02-06 12:28 (:0.0)
    Code:
    echo -e "User\tTTY\tDate\t\tTime"
    User	TTY	Date		Time
    $ who | awk 'BEGIN {OFS="\t"} {print $1,$2,$3,$4}'
    ehb	tty7	2011-02-06	12:28
    ehb	pts/0	2011-02-06	12:28
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0
    Originally Posted by SimonJM
    $ who | awk 'BEGIN {OFS="\t"} {print $1,$2,$3,$4}'
    ehb tty7 2011-02-06 12:28
    ehb pts/0 2011-02-06 12:28
    [/CODE]
    Thank you Simon, its already helping but my question is with the awk command. Ill be honest I havnt learned it yet so Im on my way to do some reading on it before I ask you questions regarding it but I wanted to at least thank you for what you gave me already!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,451
    Rep Power
    1751
    awk (not the most obvious of command names, *nix commands are usually shortened to under 8 characters or are acronyms/acrostics describing their function, awk is 'just' the initials of the original authors) is fairly simple in it's basic operation, so let's deconstruct what we are doing:

    Code:
    awk 'BEGIN {OFS="\t"} {print $1,$2,$3,$4}'
    awk has three main 'areas' BEGIN, the 'main code' and END. BEGIN code runs once, at the start of the program, END runs once at termination of program and the main code runs once per line of input. Code is grouped together inside { }.
    With tha in mind what we do is, at program start we execute the BEGIN section as we have specified one (we don't need to, but in this case it is handy) and in that all we do is {OFS="\t"} which sets an awk internal variable (OFS - Output Field Separator) to the \t (tab) character. What that will do is separate any output files with the tab character instead of the default which is a space.
    Then for each line read we output $1, $2, $3, $4, using the print command, as output fields. As we have already set the OFS to be the tab character that means they get printed with tabs.
    We don't need any final processing so we don't use the END option.
    We could, should you wish, print the headings in the BEGIN function as well:
    Code:
    BEGIN {OFS="\t"; print "User","TTY","Date","","Time"}
    Note the 'dummy' blank output to 'force' the extra tab to make the lining up correct. The semi-colon is the command separator, allowing multi-command lines.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo