awk輸出相同列的前兩行和後兩行

2021-07-17 04:00:31 字數 864 閱讀 6750

要求列印,第一列相同的頭兩行和後兩行:

file

ax  bx   1

ax  bx   2

ax  bx   1

ax  bx   8

ax  bx   1

ax  bx   3

ax  bx   5

cx  bx   1

cx  bx   0

cx  bx   1

cx  bx   6

cx  bx   9

ex  bx   1

ex  bx   a

ex  bx   1

ex  bx   1

ex  bx   1

ex  bx   6

ex  bx   1

ex  bx   1

ex  bx   1

ex  bx   1

ex  bx   x

ex  bx   y

要求輸出結果為:

ex  bx   1

ex  bx   a

ex  bx   x

ex  bx   y

ax  bx   1

ax  bx   2

ax  bx   3

ax  bx   5

cx  bx   1

cx  bx   0

cx  bx   6

cx  bx   9

awk 'end' file

解析:處理方法巧妙的地方在於利用了乙個二維的資料進行處理。兩個都是用$1作為下標,其中陣列

b中存放了,開頭為每個不同元素的資料的條數。最後的結果

b[i]

的話就是開頭是

$1的列的數量。最後進行輸出即可。

python程式的前兩行

摘抄自 一般看乙個python程式都會看到 usr bin python filename helloworld.py 作為乙個檔案的前兩行 它被稱作 組織行 原始檔的頭兩個字元是 後面跟著乙個程式。這行告訴你的linux unix系統當你 執行你的程式的時候,它應該執行哪個直譯器。chmod a ...

mysql交換兩行或兩列的值

參考 mysql left join中where和on條件的區別 新建資料庫和表,並插入資料 create database test use test create table product id int 10 unsigned not null auto increment comment 產...

Perl 輸出匹配到的某兩行之間的內容

usr bin perl open cdl,cdl or die open out,output.cdl or die while lines elsif lines ends and line control 1 if line control 1 close cdl close out usr ...