[英]Count and manipulate column variables awk or sed
我正在尝试编辑数组元素以转换#1:#2,#3,...的格式,以便#1将显示文件名,而#2-#n显示其完整文件名,并具有#2-#n的数量
<编辑>
12:12,13表示:
对于$ {names}条目“ 12”,$ {names}中存在条目,因此条目12和13是重复项
</编辑>
$ {merge}的内容
key: 0 value: 12:12,13
key: 1 value: 18:18,19
$ {name}的某些内容(仅是$ {path}的最后一部分
key: 12 value: j.smith
key: 13 value: j.smith
key: 18 value: test
key: 19 value: test
$ {path}的一些内容
key: 12 value: ./testDir/first/j.smith
key: 13 value: ./testDir/j.smith
key: 18 value: ./testDir/second/test
key: 19 value: ./testDir/third/test
我的尝试:
for ix in "${merge[@]}"
do
# Remove "#:" from "#:#,#" string and pass #,# into awk
echo ${ix} | sed s/[0-9]*:// | awk -v name="${name[*]}" '
{
FS=",";
print "\File: ",$name," Number of Matches:",NF;
for (ix=0; ix<NF; ix++)
{
print $ix,": ",$path[$ix];
};
print "END"; }'
done
这就是我得到的
File: 12,13 Number of Matches: 1
12,13 : 12,13
END
File: 18,19 Number of Matches: 1
18,19 : 18,19
END
我想要得到的是:
档案:j.smith
比赛数量:2
./testDir/first/j.smith
./testDir/j.smith
文件:测试
比赛数量:2
./testDir/second/test
./testDir/third/test
merge=([0]="12:12,13" [1]="18:18,19")
name=([12]="j.smith" [13]="j.smith" [18]="test" [19]="test")
path=([12]="./testDir/first/j.smith" [13]="./testDir/j.smith" [18]="./testDir/second/test" [19]="./testDir/third/test")
for ix in "${merge[@]}"
do
a=${ix%%:*}
b=(`echo ${ix##*:} | sed 's/,/ /g'`)
n=${name[a]}
echo
echo "File: $n"
echo "Number of Matches: ${#b[@]}"
for i in ${b[@]}
do
echo ${path[$i]}
done
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.