"合并"路径并在一行中显示所有匹配项

时间:2018-02-15 10:18:05

标签: bash shell sh

我需要帮助,我有:

/usr/include/slang.h:include
/usr/include/slang.h:star
/usr/include/slang.h:load
/usr/include/byteswap.h:include

我必须合并才能在shell中得到结果:

/usr/include/slang.h include star load
/usr/include/byteswap.h include   

我该怎么办?

1 个答案:

答案 0 :(得分:0)

$ cat merge.txt 
/usr/include/slang.h:include
/usr/include/slang.h:star
/usr/include/slang.h:load
/usr/include/byteswap.h:include

$ cat merge.sh 
#!/bin/bash

for path in $(awk -F':' '{print $1}' merge.txt | uniq)
do 
    values=$(grep "${path}" merge.txt | awk -F':' '{print $NF}' | tr '\n' ' ')
    echo "${path}" "${values}"
done

输出:

$ ./merge.sh 
/usr/include/slang.h include star load 
/usr/include/byteswap.h include