使用awk去掉檔案中某列重複的行

2021-06-27 10:21:35 字數 1211 閱讀 4789

原資料檔案test格式如下:

30/nov/2014:09:30:42    192.168.0.112_-54eb28a7_149fe14728b_-1e07|1417309903531 fi|t|tf|bbb

30/nov/2014:09:30:42    192.168.0.112_-54eb28a7_149fe14728b_-1e07|1417309903532 fi|t|tf|qqq

30/nov/2014:09:30:42    192.168.0.112_-54eb28a7_149fe14728b_-1e07|1417309903531 fi|t|tf|sdf

30/nov/2014:09:30:42    192.168.0.112_-54eb28a7_149fe14728b_-1e07|1417309903532 fi|t|tf|aaa

30/nov/2014:09:30:42    192.168.0.112_-54eb28a7_149fe14728b_-1e07|1417309903533 fi|t|tf|aaa

需要根據第二列判斷,去掉第二列重複的行

方法:由於找到不重複的key比較容易,所以先找到不重複的key生成乙個檔案,然後兩個檔案去做交集

1、找到不重複的key

cat test | awk '' | sort | uniq -c | awk '' > ~/test1

生成檔案如下

192.168.0.112_-54eb28a7_149fe14728b_-1e07|1417309903533

2、兩個檔案取交集

awk 'argind==1 argind==2 && ($2 in a) ' test1 test

輸出:30/nov/2014:09:30:42    192.168.0.112_-54eb28a7_149fe14728b_-1e07|1417309903533 fi|t|tf|aaa

命令:

awk 'argind==1 argind==2 && ($2 in a) ' test1 test

分析:argind==1 處理第乙個引數,即test1

以test1 中的第一列為索引,將test中的內容存入到a陣列中

argind==2 && ($1 in a) ' 處理第二個引數即test 檔案 ,檢查第二個檔案的第一列在資料a中就列印

oracle中去掉某列重複的記錄

建立測試表 create table temp mul tbl id varchar2 20 primary key,name varchar2 50 age int 為表和字段新增注釋 comment on table temp mul tbl is 測試表 comment on column t...

根據表中某列去除重複的行

根據表中某列 或者某些列 去除重複的行 例如有表a,有兩行相同的cardid,我們只要隨機的某一行 drop table a drop table b create table a cardid varchar 100 cardcode varchar 100 insert into a cardi...

sql DISTINCT 關鍵字去掉重複的列

distinct關鍵字主要用來從select語句的結果集中去掉重複的記錄。如果使用者沒有指定distinct關鍵字,那麼系統將返回所有符合條件的記錄組成結果集,其中包括重複的記錄。記錄一下工作中用到的sql語句吧,下面sql實現了去掉重複列id後顯示的資料。select distinct u.id,...