Text

使用 v 查找的文本到表格

  • January 6, 2020

我有如下文件

FileName:File1.txt 文本文件 我們可以得到如下表的格式嗎

需要表格格式

嘗試:

awk '
!($3 in T1)     {SQ[++CNT] = $3
               }
               {T1[$3] = T1[$3] DL[$3] $1
                T2[$3] = T2[$3] DL[$3] $2
                DL[$3] = ","
               }
END             {for (i=1; i<=CNT; i++) printf "%s\t%s\t%s\t", T1[SQ[i]], T2[SQ[i]], SQ[i]
               print _
               }
' file
1,3 2,4 5s  5   6   16s 7   8   23s 

awkGNU使用換行符或空格作為欄位分隔符的替代大錘,-F'[\n ]'這也需要RS=""以避免任何\n被視為記錄分隔符。

awk -F'[\n ]' 'BEGIN{RS=""; OFS="\t"}
   {print  substr(FILENAME,1,length(FILENAME)-4), $1","$4, $2","$5, $3, $7, $8, $9, $10, $11, $12}
   ' *.txt

輸出:

File1   1,3     2,4     5s      5       6       16s     7       8       23s

引用自:https://unix.stackexchange.com/questions/560626