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)