Find the answer to your Linux question:
Results 1 to 5 of 5
hi, i want to know how can we use command 'head' and 'tail' work in a single statement to retrieve top n bottom line of a file.individually i know but ...
  1. #1
    Just Joined!
    Join Date
    Apr 2010
    Posts
    0

    Post hi i have joined this forum just now

    hi, i want to know how can we use command 'head' and 'tail' work in a single statement to retrieve top n bottom line of a file.individually i know but how to do in a single command line.plz lets me now

  2. #2
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,091
    hi and welcome.

    The following is far from elegant and in fact downright ugly.
    head /var/log/messages > /tmp/tmp_file && tail /var/log/messages >> /tmp/tmp_file && cat /tmp/tmp_file && rm -f /tmp/tmp_file
    For more than occasional use, you might want to think about a shell wrapper, parameters, errorconditions and secure tmp file creation..
    You must always face the curtain with a bow.

  3. #3
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,091
    ah sorry, no need for a tmp file.

    this will do:
    Code:
    head /var/log/messages && tail /var/log/messages
    You must always face the curtain with a bow.

  4. #4
    Just Joined!
    Join Date
    Apr 2010
    Posts
    0

    Post thanxs

    actually i want to get process which uses maximum memory usage n minimum memory usage.
    for maximum i used
    ps -auxf | sort -nr -k 4 | head -1
    it works n
    tail -1 to get minimum
    but instead of typing two lines can i do this in a single line.

  5. #5
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,091
    You can try this:
    Code:
    ps auxf | sort -nr -k 4|pee "head -1" "tail -1"
    The "pee" command is part of the moreutils package.


    P.S.
    The coffee lounge seems not the best place for questions like these.
    It would have been better placed in Linux Applications.
    Well, next time
    Last edited by Irithori; 04-02-2010 at 02:47 PM.
    You must always face the curtain with a bow.

Posting Permissions

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