awk使用详解
倒数列:
awk -F “:” ‘{print
2
,
2,
2,(NF),
(
N
F
−
1
)
,
(NF-1),
(NF−1),(NF-3)}’ /etc/passwd
行转列:
转为行后,去掉最后一个逗号:
awk ‘{printf “%s,”,KaTeX parse error: Expected 'EOF', got '}' at position 2: 1}̲' a1.txt|sed 's…//’
awk ‘{printf “%s,”,KaTeX parse error: Expected 'EOF', got '}' at position 2: 1}̲' a1.txt | awk …/,“”)}1’
awk ‘{printf “%s,”,$1}’ a1.txt | awk ‘{printf $0"\b \n"}’
sort b.txt|uniq|awk ‘{a[$1]=(a[$1]" "$2);} END{for(i in a) print i ":"a[i]}’
多行转为一行:cat ip.list|xargs|sed -i ‘s/ /,/g’
不打印第一行:
awk ‘NR!=1 {print $1,$2}’ test.txt
-
匹配与不匹配:
-
匹配正则 !~ 不匹配正则
== 等于 != 不等于
打印第二列中不匹配80开头并以80结束的行:awk ‘{if( 2 ! / 8 0 2!~/^80 2! /80/)print}’ test.txt
打印第二列是"bo"的行:cat test.txt |awk ‘{if($2==“bo”)print}’
内置变量:
NF 字段个数,(读取的列数)
NR 记录数(行号),从1开始,新的文件延续上面的计数,新文件不从1开始
FNR 读取文件的记录数(行号),从1开始,新的文件重新从1开始计数
FS 输入字段分隔符,默认是空格
OFS 输出字段分隔符 默认也是空格
RS 输入行分隔符,默认为换行符
ORS 输出行分隔符,默认为换行符
读取记录的字段数(列数):awk -F" " ‘{print "字段数: " NF}’ test
对文件的第三列进行去重:
awk ‘{a[$3]=$0}END{for(i in a)print a[i]}’ test.txt
hdfs dfs -ls /user/hive/warehouse/ods_mes_standard.db/ods_workorder_df_hive_v2/*/ | awk ‘{print KaTeX parse error: Expected 'EOF', got '}' at position 3: NF}̲'| awk -F "=" -…NF<a {print $0}’ |xargs hdfs dfs -rm -r
hdfs dfs -ls /user/hive/warehouse/ods_mes_standard.db/ods_tracking_di_hive/*/ | awk ‘$NF !~ year=2024 {print $NF}’ | grep -v “items” |xargs hdfs dfs -rm -r
hdfs dfs -ls /user/hive/warehouse/proj_30_manufacture_delivery.db/dwd_std_tracking_log_base_new/// | awk ‘{print KaTeX parse error: Expected 'EOF', got '}' at position 3: NF}̲'| awk -F "=" -…NF<a {print $0}’ |xargs hdfs dfs -rm -r
set hive.msck.path.validation=ignore;
msck repair table ods_mes_standard.ods_tracking_di_hive;