我有一个包含 3 个动态列的源文件,我想根据分隔符对其进行拆分。
下图显示了我的源文件和所需的输出;两者也转载为以下文本:
来源:
Emp,Hire,ILT,Mgr,Pi
123,Y,UB|VY,,BY|LN|VB
435,N,LN,HA-12/09/2019|LA-13/09/2019,BY|LN|OR
657,Y,,MA-12/09/2019|RA-13/09/2019,VB
期望的输出:
Emp,hire,ILT,Mgr,Pi
123,Y,UB,,BY
123,Y,VY,,LN
123,Y,,,VB
435,N,LN,HA-12/09/2019,BY
435,N,,LA-13/09/2019,LN
435,N,,,OR
657,Y,,MA-12/09/2019,VB
657,Y,,RA-13/09/2019,VB
答案1
假设您将此 AWK 脚本保存为script
:
BEGIN {
# Populate an array that lists (is indexed
# by) the position of "dynamic" fields.
split(dynamic,temp,",")
for (i in temp)
tosplit[temp[i]] = i
}
{
# Determine how many times the current
# line will be repeated...
times = 1
# by counting how many times, for each field,
for (f = 1; f <= NF; f++) {
# the "|" separator is replaced by itself.
repl = gsub(/\|/, "&", $f)
if ( (repl + 1 ) > times)
times = (repl + 1 )
}
# For each time the line has to be repeated:
for (i = 1; i <= times; i++) {
for (f = 1; f <= NF; f++) {
# every "dynamic" field is split on "|", and
# only the component which belongs to the
# current line repetition is printed;
if (f in tosplit) {
split($f, p, "|")
printf( (f == NF ? "%s"ORS : "%s"OFS), p[i] )
}
# all other fields are printed unchanged.
else
printf( (f == NF ? "%s"ORS : "%s"OFS), $f )
}
}
}
然后您可以将其调用为:
awk -v FS=',' -v OFS=',' -v dynamic=3,4,5 -f script source_file
awk
需要拆分的列(“动态”字段)的索引作为保存逗号分隔列表的变量传递。