Shell script, awk merging a column of issues.

Source: Internet
Author: User

The file file2 content is as follows:
0 Qwert
1 ASDFGHJK
2 ASDJKL
2 zxcvbn
3 Dfghjkll
4 22222
4 TYUIOP
4 BNM

Make the first column equal to one row, not the first column, that is, the following format:
22222;tyuiop;bnm
Qwert
Asdfghjk
Asdjkl;zxcvbn
Dfghjkll

The first method of solution

Problem Solving Ideas:

awk ' {if (a[$1]) {a[$1]=a[$1] ";" $2}else{a[$1]=$2}}end{for (i in a) print A[i]} ' file2

File2

0 Qwert
1 ASDFGHJK
2 ASDJKL
2 zxcvbn
3 Dfghjkll
4 22222
4 TYUIOP
4 BNM

First line if (A[0]) because a[0] does not have any value, so the output is directly else a[0]= Qwert

The second line if (a[1]) because a[1] does not have any value, so the output is directly else a[1]= ASDFGHJK

The third line if (a[2]) because a[2] does not have any value, so the output is directly else a[2]= ASDJKL

Line four if (a[2]) because a[2]= ASDJKL, meet the conditions, execute a[$1]=a[$1] ";" $----> a[2]=a[2] ";" $--->a[2]=asdjkl;zxcvbn

Line five if (a[3]) because a[3] does not have any value, so direct output else a[3]= dfghjkll

Line six if (A[4]) because A[4] does not have any value, so direct output else a[4]= 22222

Line seventh if (A[4]) because a[4]= 22222, meet the conditions, execute a[$1]=a[$1] ";" $----> a[4]=a[4] ";" $--->A[4]=22222;TYUIOP

Line eighth if (A[4]) because a[4]= 22222;TYUIOP, meet the conditions, execute a[$1]=a[$1] ";" $----> a[4]=a[4] ";" $--->a[4]=22222;tyuiop;bnm

The final result after the entire file cycle is:

a[0]= Qwert

a[1]= ASDFGHJK

A[2]=asdjkl;zxcvbn

a[3]= Dfghjkll

A[4]=22222;tyuiop;bnm

End{for (i in a) print A[i]}

Use for to go to array a subscript 0 1 2 3 4

Then respectively print a[0] a[1] a[2] a[3] a[4]

(because the index of the extracted array is an irregular output, the result may be a random sort)

The second method of solution

Shell script, awk merging a column of issues.

Related Article

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.