Creative Commons License
This blog by Tommy Tang is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

My github papge

Monday, March 3, 2014

Unix sort except the first line

Many times I have a file with a header  needs  to be sorted, but I do not want to sort the header.
I saw it on Twitter:

 Retweeted by 
todays fav linux oneliner: command | (read -r; printf "%s\n" "$REPLY"; sort) > output sort everything except first line.

REPLY is the default variable for the read command.

one more general use of the "body" function
http://stackoverflow.com/questions/9281449/unix-skip-header-bash-function
http://unix.stackexchange.com/questions/11856/sort-but-keep-header-line-in-the-at-the-top

# print the header (the first line of input)
# and then run the specified command on the body (the rest of the input)
# use it in a pipeline, e.g. ps | body grep somepattern
body() {
    IFS= read -r header
    printf '%s\n' "$header"
    "$@"
}
IFS is the bash Shell Bourne variable: A list of characters that separate fields used by the shell to split text strings.

$#    Stores the number of command-line arguments that 
      were passed to the shell program.
$?    Stores the exit value of the last command that was 
      executed.
$0    Stores the first word of the entered command (the 
      name of the shell program).
$*    Stores all the arguments that were entered on the
      command line ($1 $2 ...).
"$@"  Stores all the arguments that were entered
      on the command line, individually quoted ("$1" "$2" ...).

Or an awk solution awk 'NR == 1; NR > 1 {print $0 | "sort -n"}'
It is very handy to use.

Test:
tommy@tommy-ThinkPad-T420:~$ cat body_sort.txt 
header
4
6
9
7
14
8

tommy@tommy-ThinkPad-T420:~$ cat body_sort.txt |( read -r; printf "%s\n" "$REPLY"; sort -n)
header
4
6
7
8
9
14





13 comments:


  1. Wow!!! It was really an Informational Article which provide me with much Insightful Information. I would like to first thank the author for such an Insightful Content. If someone here is looking for a Website Designing Company in India, then look no further than Jeewangarg.com as we are the best website design company in Delhi working in the arena for the last 8 years.
    Best website designing company in India
    website designing company in Delhi NCR

    ReplyDelete
  2. Thank You Author!!! You are sharing the very informative and great post. If anyone is looking to lehenga indian wedding dresses | lehenga online .

    ReplyDelete
  3. An excellently written article if only all blogger offered the same level of content as you the internet would be a much better place. Please keep it up!. Great tips, please check this one 123movies

    ReplyDelete
  4. Thanks for provide great informatic and looking beautiful blog, really nice required information & the things i never imagined and i would request, wright more blog and blog post like that for us. Thanks you once agian
    we offer services delhi birth certificate which inculde name add in birth certificate and birth certificate correction complete process is online and we offer
    birth certificate and we offer this birth certificate online same service offers at yourdoorstep at birth certificate in ghaziabad our dream to provide birth certificate in india and other staes like birth certificate in bengaluru and birth certificate in gurgaon book service with us birth certificate in noida also, service at yoursdoorstep only birth certificate in india or

    ReplyDelete
  5. Online Front is a professional digital marketing company in Delhi. You can get the best digital marketing services by online front. We have an expert team of digital marketing who make the best strategy to promote your business through Google, Facebook, Instagram, and other social media platforms. We know it might be tough to achieve the target but not impossible. And our team proved! you can check our previous work with our previous client.

    Visit For More Info:-

    Website Development Company In Delhi
    Digital Marketing Company In Delhi
    Website Development Packages
    Website Designing company in delhi
    Social Media Management Packages
    Email Marketing Services

    Visit Our Partner’s Site:-

    Gas Stove Repair & Services in Delhi
    B2B Marketplace in Delhi
    Satta King

    ReplyDelete
  6. **Hello my dear !!! I'm Kunjan and I want to thank you for visiting my profile. My City Escort Services are TOP quality and I can guarantee your satisfaction. The reason is that I do City Escort job with pleasure and I enjoy every part of it. With my beautiful lips, big deep eyes, and brown soft skin you can turn your dreams into reality!!! My pictures are recent, I'm Real and I don't waste time to hang out with this Sexy City Escort. Take a break from your stressful lifestyles and enjoy a relaxing time with me. I promise you, all the moments spend with me will make you come back for more. Sometimes I can be crazy and evil as hell!! I'm 100% independent!! Give me a call anytime will be fun and nice for both of us! as a City Call Girl.
    Mahendragarh Call Girls !!
    Karnal Call Girls !!
    Kaithal Call Girls !!
    Jind Call Girls !!
    Jhajjar Call Girls !!
    Fatehabad Call Girls !!
    Faridabad Call Girls !!
    Charkhi Dadri Call Girls !!
    Bhiwani Call Girls !!

    ReplyDelete
  7. Nice blog, with very relevant information, get back to us for some inspiring content. We are also serving in the same domain delivering top-notch mobile app development solutions. For more details, visit iphone app development company or contact us: +91-9717270746 or email us: sales@appsquadz.com

    ReplyDelete
  8. Hello gentlemen, the name is Sneha and I am a genuine 23-year-old independent call girl. I am willing to do everything with great affection and offer you for our delicious sex encounter.
    Relationship
    Call girls
    Call girl
    Female
    Call girls for party
    Hotel call girls
    Model
    Incall girls
    Outcall girls
    Escort service
    Female call girls service
    Erotic massage
    Call girls number
    Call girl number
    Call girls WhatsApp
    Call girl WhatsApp

    ReplyDelete
  9. This post is so interactive and informative.keep update more information...
    AWS Training in Anna Nagar
    AWS Training in Chennai

    ReplyDelete
  10. It's remarkable. The way you describe the information is awesome. This will really help me out. Thanks for sharing.
    Tableau Training in Chennai
    Tableau Course in Chennai
    Tableau Online Course

    ReplyDelete
  11. Every one deserves to be happy in their family or relationships, I was lucky enough to get my husband back , Doctor Robbinson helped me he is authentic , My Husband left me for 8 months and I was devastated and couldn't find any way out to get him back i tried all ways. At some point I was depressed, while i was reading through some post on the internet where people testified about how Dr Robbinson helped them Solve similar problems I contacted him and he never hesitated to help me as well, My husband is back home, my happiness is restored
    Text/call: +12267705795
    Email: Drrobbinssonspiritualhome@gmail.com

    ReplyDelete