Linux text row and column conversions

Source: Internet
Author: User

Use awk for row and column conversions

Suppose you have a file test that contains data that follows the following format:

Same row of data, 1 spaces between words ""

Each row has the same amount of data, the same number

Processing commands:

awk ' {for (i=1;i<=nf;i=i+1) {a[nr,i]= $i}}end{for (j=1;j<=nf;j++) {str=a[1,j];for (i=2;i<=nr;i++) {STR=STR "" a [I,j]} Print str} ' test

Brief description:

Nr-number of Record-the line that is currently being processed is the number of rows (because awk is a stream processing tool, one row at a line, so NR is constantly increasing by 1); the Nr referenced in the end is the Nr after the text is processed

Fnr-file number of Record-the line currently being processed is the first line of the currently processed file

Nf-number of Fileds-How many column data the current row has (this is recalculated in each row according to the set delimiter, and the default delimiter is any contiguous number of whitespace characters)

Brief analysis of processing process:

The first step is to put the data in the text into a 2-dimensional array, stored in a manner identical to the text;

The column data is obtained when awk processes the text, and the Nr indicates that a few lines of text are processed and the output has a NR column; NF indicates the number of columns in the text and the output is NF.

Processing 2-dimensional arrays in the end module, converting rows of arrays into columns and depositing into another 2-D array str

Finally print out this array str

Linux text row and column conversions

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.