下面是数据样本。
我想要实现的是,我必须计算每行中的逗号数量(另外我必须忽略双引号(“”)中的字符串的逗号)。如果逗号的数量小于特定数量,那么我必须添加剩余逗号的数量。例如。每行的逗号总数应为 50。如果一行有 18 个逗号,那么我必须在行尾添加剩余的 32 个逗号。另外,我必须忽略双引号(“”)中的字符串的逗号。
[20/07/2016 23:56:13.412],1502740051,2,501,"[2]",8,TL_mostPlayedVOD_15_days,
[20/07/2016 23:56:13.412],1502740051,2,501,"[2]",8,TL_mostPlayedVOD_15_days,,2,1645981,"you, me and he",1461742773000
[04/10/2016 01:30:30.789],1502702627,2,501,"[2]",8,TL_mostPlayedVOD_15_days,,2,1967748,"sex and the city",1414665744000,1512143880000,NAM20141030102723000,2,3967956,"the big bang theory",1469531668000,1492185539000,NAM20160726104146000,2,6399683,"abang bomba ,i love you",1473865200000,1491148680000,NAM20160914094137000,2,6226697,"super spontan superstar",1471068000000,1491321540000,NAM20160813044535000,2,6579649,"lara aishah",1474887600000,1499702340000,NAM20160925025431000,2,6671628,"konsert af 2016",1474869600000,1491494160000,NAM20160928025259000,
[04/10/2016 01:30:30.789],1502702627,2,501,"[2]",8,TL_mostPlayedVOD_15_days,,2,1967748,"sex and the city",1414665744000,1512143880000,NAM20141030102723000,2,3967956,"the big bang theory",1469531668000,1492185539000,NAM20160726104146000,2,6399683,"abang bomba i love you",1473865200000,1491148680000,NAM20160914094137000,2,6226697,"super spontan,superstar",1471068000000,1491321540000,NAM20160813044535000
答案1
感谢大家的宝贵意见和建议。刚刚找到一个更简单的解决方案。
#!/bin/bash
FILE='/tmp/file.log'
cat $FILE | awk '{print $0",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"}' >> MODIFIED_FILE
while read line;
do
echo $line | cut -d ',' -f1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50 >> NEW_FILE
done < "MODIFIED_FILE"
答案2
以下是一些可以解决问题的代码awk
:sed
首先创建一个 bash 数组:
array=()
数组的每个元素在每行上填充“,”的数量,不包括用引号括起来的那些(假设您的行不以引号开头!)
num=$(cat myData.txt | wc -l) for ((i=1; i <= $num; i++)); do array[$i]=$(cat myData.txt | awk 'BEGIN{FS="\""}{for (i=1; i <= NF; i=i+2) printf $i; print ""}' | sed 's/[^,]//g' | awk -v i=$i 'NR=i{printf length}'); done
定义每行“,”的最大数量,并使用最终的 awk 命令使用我们之前构建的数组在每行末尾打印正确的“,”数量
numMax=50 awk -v array="${array[*]}" -v max=$numMax 'BEGIN{split(array,a," ")}{printf $0; for (i=a[NR]; i < max; i++) printf ","; print ""}' myData.txt > myProcessedData.txt
以下是脚本形式的整个过程:
#!/bin/bash
array=()
numMax=50
num=$(cat myData.txt | wc -l)
for ((i=1; i <= $num; i++))
do
array[$i]=$(cat myData.txt | awk 'BEGIN{FS="\""}{for (i=1; i <= NF; i=i+2) printf $i; print ""}' | sed 's/[^,]//g' | awk -v i=$i 'NR=i{printf length}')
done
awk -v array="${array[*]}" -v max=$numMax 'BEGIN{split(array,a," ")}{printf $0; for (i=a[NR]; i < max; i++) printf ","; print ""}' myData.txt > myProcessedData.txt