我在下面用逗号分隔了两列:
"01962424813","KSDP01"
"KSDP01","01962424813"
"8801962424813","KSDP01"
01962424813,KSDP01
1962424813,KSDP01
8801962424813,KSDP01
KSDP01,01962424813
KSDP01,1962424813
KSDP01,8801962424813
"1962424813","KSDP01"
"KSDP01","1962424813"
"KSDP01","8801962424813"
01959969994,ASDP11
"8801962424183","ASDP01"
"801962424183","KSDP11"
"01962424183","CSDP10"
"1962424183","ASDP09"
8801962424183,CSDP13
801962424183,KSDP08
01962424183,ASDP07
1962424183,CSDP05
"ASDP01","8801962424183",
"KSDP11","801962424183",
"CSDP10","01962424183"
"ASDP09","1962424183"
CSDP13,8801962424183
KSDP08,801962424183
ASDP07,01962424183
CSDP05,1962424183
01912401927,CSDP06
每当我在第一列中找到子串19
时,我想交换这两个值。我怎样才能做到这一点?这是我尝试过的:
awk -F, 'BEGIN{OFS=","}{$1=$1;if( awk '$1 ~ /19/' ){print $2,$1}else{print $1,$2}}'
答案 0 :(得分:1)
awk -v FS="," -v OFS="," '($1 ~ /19/){temp=$1;$1=$2;$2=temp}1' filename
应该这样做。以下是示例输出
"KSDP01","01962424813"
"KSDP01","01962424813"
"KSDP01","8801962424813"
KSDP01,01962424813
KSDP01,1962424813
KSDP01,8801962424813
KSDP01,01962424813
KSDP01,1962424813
KSDP01,8801962424813
"KSDP01","1962424813"
"KSDP01","1962424813"
"KSDP01","8801962424813"
ASDP11,01959969994
"ASDP01","8801962424183"
"KSDP11","801962424183"
"CSDP10","01962424183"
"ASDP09","1962424183"
CSDP13,8801962424183
KSDP08,801962424183
ASDP07,01962424183
CSDP05,1962424183
"ASDP01","8801962424183",
"KSDP11","801962424183",
"CSDP10","01962424183"
"ASDP09","1962424183"
CSDP13,8801962424183
KSDP08,801962424183
ASDP07,01962424183
CSDP05,1962424183
CSDP06,01912401927