Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format data to table format

Tags:

bash

I am a total novice to coding, but am wondering the easiest way to generate a table from grep count data.

My grep count output file looks like this:

AAR34355.1
./006D_id70.m8:0
./20D_id70.m8:0
./28D_id70.m8:0
AAR38850.1
./006D_id70.m8:0
./20D_id70.m8:2
./28D_id70.m8:4
A13520.1
./006D_id70.m8:0
./20D_id70.m8:0
./28D_id70.m8:0

I need an output to look more like this:

            ./006D_id70.m8    ./20D_id70.m8    ./28D_id70.m8
AAR34355.1         0                0                 0
AAR38850.1         0                2                 4
A13520.1           0                0                 0

or at least a delimited equivalent.

Forgive my description, as I am pretty new to this.

Is there a relatively simple way of formatting the data this way?

like image 908
JJV Avatar asked Mar 18 '20 09:03

JJV


People also ask

Can you format the data in an Excel spreadsheet to look like a table?

Select a cell within your data. Select Home > Format as Table. Choose a style for your table. In the Format as Table dialog box, set your cell range.

Why do we format data as a table?

Formatting your range as a table tells Excel that those rows and columns are all related, and that there are headers in the first row. And by doing this, your range now has meaning. Excel understands it better. And with that, lots of additional benefits are born.

What is a table format in Excel?

When you use Format as Table, Excel automatically converts your data range to a table. If you don't want to work with your data in a table, you can convert the table back to a regular range while keeping the table style formatting that you applied.


1 Answers

You can do that all in awk, no need to reshape grep's output. Assuming patterns to be searched for are listed in a file named patterns, and files to be searched in are file1, file2, and file3; copy and save the following code block into a file named tst.awk,

NR == FNR {
  pat[NR] = $0
  next
}

FNR == 1 {
  fil[c++] = FILENAME
}

{
  for (i in pat)
    if ($0 ~ pat[i])
      mat[FILENAME, pat[i]]++
}

END {
  for (i in fil)
    printf "\t%s", fil[i]

  print ""

  for (i in pat) {
    printf "%s", pat[i]

    for (j in fil)
      printf "\t%d", mat[fil[j], pat[i]]

    print ""
  }
}

and run

awk -f tst.awk patterns file1 file2 file3

Demo:

$ seq 5 > file1
$ seq 3 7 > file2
$ seq 5 9 > file3
$ seq 3 2 7 > patterns
$ awk -f tst.awk patterns file1 file2 file3
        file1   file2   file3
3       1       1       0
5       1       1       1
7       0       1       1
like image 73
oguz ismail Avatar answered Oct 17 '22 14:10

oguz ismail