Some usage records of awk (update ing)

Source: Internet
Author: User

Please use the awk command to merge the two lines with the same name in the following two files. A file: Sun Xing 50-year-old Walker Sun 500-year-old sun 5,000-year-old grandson line 50,000-year-old B file: Walker Sun male Sun Xing male grandson man-in-the-line male output effect: Sun Xing 50-year-old male
[[email protected] ~]$ Cat a b |sort-k1 |awk ' begin{t= ""}{t=$1;if (tt==t) {printf ("%s", $ $);p rintf ("\ n")}else{printf ("%s% S ", $1,$2);} Tt=t} ' sun 50,000-year-old male Sun Xing 50-year-old man-Sun 5,000-year-old male Walker Sun 500-year-old male

Idea: Merge two files and sort them first

[[email protected] ~]$ Cat a b |sort-k1 sun 50,000-year-old Sun man Sun Xing 50-year-old Sun Xing man-Sun 5,000-year-old person row Sun male Walker Sun 500-year-old Walker Sun male
First define a container for $ $, which is the name, and then make a judgment if the next line of $ $ is the same as this, only the output is $, and the line is wrapped. Because the definition is empty, it does not conform so judge the else output $1,$2 that is the grandchild line 50000. After that, the same only outputs the $ $ and wraps. If the output is Sun Xing, 50-year-old man,

What do we do then?

Cat a B |sort-k1 |awk ' begin{t= ""}{t=$1;if (tt==t) {printf ("\ t");p rintf ("%s", $ $);p rintf ("\ n")}else{printf ("%s%s", $1,$ 2);p rintf ("\ n")}tt=t} ' sun 50,000-year-old male Sun Xing 50-year-old man-Sun 5,000-year-old male Walker Sun 500-year-old male train of thought is actually the same.


This article is from the "tireless learning ..." Blog, be sure to keep this source http://jonyisme.blog.51cto.com/3690784/1671260

Some usage records of awk (update ing)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.