字段處理
1).使用cut選定字段
cut 語法
cut -c list [file...]
cut -f list [-d delim] [file...]
-c list :
以字元為主,執行剪下操作
-f list :
以欄位為主,執行剪下操作
-d delim :
通過-f選項,以delim作為定界符,預設為空格
例項:
[gz_fieldyang@ test ~]$ ls -l | grep example
-rw-rw-r-- 1 gz_fieldyang gz_fieldyang
50 mar
4 16:06 example.txt
[gz_fieldyang@ test ~]$ ls -l |grep example|cut -c 1-5,40-50
-rw-r 50 mar
4#取1-5,40-50兩段
[gz_fieldyang@ test ~]$ cat /etc/passwd |grep field
gz_fieldyang:x:859:859::/home/gz_fieldyang:/bin/bash
[gz_fieldyang@ test ~]$ cut -d: -f 1 /etc/passwd |grep field
gz_fieldyang
#以分號為定界符取第一段
[gz_fieldyang@ test ~]$ cut -d: -f 6 /etc/passwd |grep field
/home/gz_fieldyang
[gz_fieldyang@ test ~]$ cut -d: -f 2-6 /etc/passwd |grep field
x:859:859::/home/gz_fieldyang
#以分號為定界符取第二到六段
[gz_fieldyang@ test ~]$ cut -d: -f 2,6 /etc/passwd |grep field
x:/home/gz_fieldyang
[gz_fieldyang@ test ~]$
2).使用join連線字段:
用法:以共同乙個鍵值,將已儲存檔案內的記錄加以結合
找出兩個檔案中,指定字段內容相同的行,並加以合併,再輸出到標準輸出裝置
join語法
join [option...] file1 file2
join [-i][-a<1或2>][-e《字串》][-o《格式》][-t《字元》][-v<1或2>][-1《字段》][-2《字段》][--help][--version][檔案1][檔案2]
常用引數說明:
-a<1
或2>
除了顯示原來的輸出內容之外,還顯示指令檔案中沒有相同欄位的行。
-e<
字串》
若[檔案1]與[檔案2]中找不到指定的字段,則在輸出中填入選項中的字串。
-i或--igore-case
比較字段內容時,忽略大小寫的差異。
-o<
格式》
按照指定的格式來顯示結果。
-t<
字元》
使用欄位的分隔字元。
-v<1
或2>
跟-a相同,但是只顯示檔案中沒有相同欄位的行。
-1<
字段》
連線[檔案1]指定的字段。
-2<
字段》
連線[檔案2]指定的字段。
--help
顯示幫助。
--version
顯示版本資訊。
例項:建立檔案發file1和file2內容如下
[gz_fieldyang@ test ~]$ cat > sales
# 業務員資料
# 業務員量
kin_ma
210
jones_yan
212
field_yang 118
will_wu
260
[gz_fieldyang@ test ~]$ cat > quotas
#配額
#業務員配額
kin_ma
102
jones_yan
132
field_yang
263
will_wu
282
[gz_fieldyang@ test ~]$ join quotas sales
will_wu 282 260
#未刪除注釋,結合最後一行
#刪除注釋後,可結合所有資料
[gz_fieldyang@ test ~]$ join sales quotas
kin_ma 210 102
jones_yan 212
132
field_yang 118
263
will_wu 260 282
[gz_fieldyang@ test ~]$
例項:結合兩個檔案的指令碼
[gz_fieldyang@ test ~]$ cat > merge-sales.sh
#!/bin/bash
#merge-sales.sh
## 結合配額與業務員資料
# 刪除資料並排序資料檔案
sed '/^#/d'quotas | sort >quotas.sorted
sed '/^#/d'sales | sort >sales.sorted
#以第乙個鍵值做結合,將結果產生至標準輸出
join quotas.sorted sales.sorted
# 刪除快取檔案
rm quotas.sorted sales.sorted
[gz_fieldyang@ test ~]$ chmod +x merge-sales.sh
[gz_fieldyang@ test ~]$ ./merge-sales.sh
field_yang 263
118
jones_yan 132 212
kin_ma 102 210
will_wu 282 260
資料處理之欄位拆分
coding utf 8 1.概念 字段拆分,是指按照固定的字元,拆分已有字串 字元分割函式 split sep,n,expand false sep 用於分割的字串 n 分割為多少列 expand 是否展開為資料框,預設為false,但我們往往會設定為true 返回值 如果expand為true,...
awk 字段處理
基本用法 awk 條件動作1 條件動作2 filename概述 1.awk為行處理字段,預設字段分割為空格 或 tab 2.表示動作 command 條件型別置於 外,最後用單引號 括住 3.當條件型別為if語句時,要置入動作內 4.awk所有動作中,中,若有多個輔助命令,可用 或enter隔開 5...
Ntext欄位拆分處理
if exists select from dbo.sysobjects where id object id n dbo p split and objectproperty id,n isprocedure 1 drop procedure dbo p split go ntext欄位拆分處理 ...