Reputation: 41
I am unable to catch numbers from file1 from column $3, where should be met condition, that just only numbers in length of exact 8 numbers before underscore or after underscore are extracted.
This below doesn't work. Only 2nd is a bit close to solution:
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
Needed output:
49989798797 mach 19800511
498797877 pat 12345678
44444 def 12345678
5659 explaination 28008874
5659 explaination 28008874
Upvotes: 1
Views: 67
Reputation: 88989
With GNU awk. Split last field ($NF
) with _
in an array and use a for loop with its elements. In every loop check content of current element with a regex.
awk '{split($NF,array,"_"); for(i in array) { if(array[i]~/^[0-9]{8}$/) { print $1,$2,array[i]; next } } }' file
Output:
49989798797 mach 19800511 498797877 pat 12345678 44444 def 12345678 5659 explaination 28008874 5659 explaination 28008874
See: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR
Upvotes: 2