您能否根据您显示的示例尝试以下操作,这些示例仅在 GNU awk 中编写和测试。根据 OP 的描述,使用正则表达式在行中匹配 3 digits space 2 digits/2 digits 模式。
awk '
BEGIN{
OFS="\t"
}
match($0,/[0-9]{3} [0-9]{2}\/[0-9]{2}.*/){
firstPart=substr($0,1,RSTART-1)
sub(/[^ ]* +/,"",firstPart)
restPart=substr($0,RSTART,RLENGTH)
sub(/ +/,OFS,restPart)
print $1,firstPart,restPart
}
' Input_file | column -t -s $'\t'
输出如下。
2531 POKRZYWNIAK KRZYSZTOF 244 18/01 2 13:46 23:26
3346 SOROTA DARIUSZ 244 18/01 1 04:05 13:46
说明:为上述解决方案添加详细说明。
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section of this program from here.
OFS="\t" ##Setting output field separator as TAB here.
}
match($0,/[0-9]{3} [0-9]{2}\/[0-9]{2}.*/){ ##Using match function to match 3 digits space 2 digits/2 digits.
firstPart=substr($0,1,RSTART-1) ##Creating firstPart which has sub string from 1st position to till RSTART-1
sub(/[^ ]* +/,"",firstPart) ##Substituting till space everything with NULL in firstPart here.
restPart=substr($0,RSTART,RLENGTH) ##Creating restPart with substring of matched regex in match function.
gsub(/ +/,OFS,restPart) ##Globally Substituting spaces with TAB in restPart.
print $1,firstPart,restPart ##Printing first field, firstPart and restPart here.
}
' Input_file | column -t -s $'\t' ##Mentioning Input_file and sending awk output to column command to get good output.