我无法从 file1 的第 3 列中提取数字,其中应该满足的条件是,只提取下划线前或下划线后的长度正好为 8 的数字。
下面这个不能用。只有第2个有点接近解决方案。
awk '{gsub(/[^0-9]{8}/,"",$3) && $22 = substr($3,1,8)}1' file1
awk '{gsub(/[^[:digit:]]/,"",$3) && $3 = substr($3,1,8)}1' file1
file1:
44446464654 field 2020135_7777_5x_definition
49989798797 mach 202013_7777_a5_19800511
498797877 pat math_200418_memo05874_12345678
44444 def math_200418_memo05874789_12345678
5659 explaination 28008874_memo
5659 explaination _28008874_memo
需要输出:
49989798797 mach 19800511
498797877 pat 12345678
44444 def 12345678
5659 explaination 28008874
5659 explaination 28008874
分割最后一个字段($NF
)与 _
的数组中,并对其元素使用for循环。在每一次循环中,用regex检查当前元素的内容。
awk '{split($NF,array,"_"); for(i in array) { if(array[i]~/^[0-9]{8}$/) { print $1,$2,array[i]; next } } }' file
輸出結果
49989798797 mach 19800511498797877 pat 1234567844444 def 123456785659 explaination 280088745659 explaination 28008874