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





33 comments:

  1. This blog was really amazing, thank you so much for sharing this blog.
    Motorcycle Tours in India

    ReplyDelete
  2. Thanks for giving great kind of information. So useful and practical for me. Thanks for your excellent blog, nice work keep it up thanks for sharing the knowledge.
    Best Interior Designer in Noida

    ReplyDelete
  3. nice work keep it up thanks for sharing the knowledge.Thanks for sharing this type of information, it is so useful.
    Epoxy Grout manufacturer

    ReplyDelete
  4. I feel happy about and learning more about this topic. keep sharing your information regularly for my future reference. This content creates a new hope and inspiration with in me. Thanks for sharing article like this. the information which you have provided is better then other blog.
    Best IELTS Coaching in Dwarka sector 12

    ReplyDelete
  5. Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it. You’re doing a great job Man,Keep it up.
    Study Abroad in New Zealand

    ReplyDelete

  6. 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
  7. Thank You Author!!! You are sharing the very informative and great post. If anyone out there is looking to book manali tour package | Rajasthan Tour Packages then don’t go beyond Indian Travel Store – “Your Trusted Partner in your Travel Journey.”

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

    ReplyDelete
  9. Thanks for the great article and post. Need a Industrial UPS? Don’t go beyond nexusups. “A popular Industrial UPS which offers an extensive range of exceptional Industrial UPS, and Online UPS Free to contact.

    ReplyDelete
  10. ftv is a Porn Training site. ftv is the largest Amateur porn training institute with the hottest selection Online and offline real life cam.
    sex video

    ReplyDelete
  11. 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
  12. Your style is very unique compared to other folks I've read stuff from. Many thanks for posting when you have the opportunity, blog Guess I will just book mark this site.
    Selenium Training in chennai | Selenium Training in anna nagar | Selenium Training in omr | Selenium Training in porur | Selenium Training in tambaram | Selenium Training in velachery

    ReplyDelete
  13. 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
  14. Sicaro is an Construction Project Management Company in Australia. With a paramount focus on the Development, Designing, Engineering and Construction of Residential, Commercial, and Infrastructure projects, Sicaro uses a range of expertise in construction project management, engineering and architecture to deliver exceptional results within scope and budget.

    ReplyDelete
  15. 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
  16. Ripple Film specialises in Corporate Video Production. We also offer digital advertising services including Google and Facebook and generate real, tangible results. We help businesses rapidly grow using video production and digital advertising systems.

    ReplyDelete
  17. **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
  18. 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
  19. 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