缩进的线条(树)到路径的线条

时间:2014-03-25 22:41:54

标签: bash recursion tree

我输入的文件结构如下:

a1
  b1
    c1
    c2
    c3
  b2
    c1
      d1
      d2
  b3
  b4
a2
a3
  b1
  b2
    c1
    c2

每个级别缩进2个空格。所需的输出是:

a1/b1/c1
a1/b1/c2
a1/b1/c3
a1/b2/c1/d1
a1/b2/c1/d2
a1/b3
a1/b4
a2
a3/b1
a3/b2/c1
a3/b2/c2

它就像一个文件系统,如果下一行有更大的缩进,当前的那个就像一个“目录”,当有相同的缩进时,它就像一个“文件”。需要打印“文件”的完整路径。

尝试在没有任何高级语言的情况下解决此问题,例如pythonperl - 只使用基本的bash命令。

我当前的代码/想法是基于递归函数调用和使用堆栈,但是“逻辑”有问题。代码目前输出下一个:

a1 b1 c1
a1 b1
a1

DD: line 8: [0-1]: bad array subscript

只有第一行是正确的 - 所以处理递归是错误的......

input="ifile.tree"

#stack array
declare -a stack

#stack manipulation
pushstack() { stack+=("$1"); }
popstack() { unset stack[${#stack[@]}-1]; }
printstack() { echo "${stack[*]}"; }

#recursive function
checkline() {
    local uplev=$1

    #read line - if no more lines - print the stack and return
    read -r level text || (printstack; exit 1) || return

    #if the current line level is largest than previous level
    if [[ $uplev < $level ]]
    then
        pushstack "$text"
        checkline $level    #recurse
    fi

    printstack
    popstack
}

# MAIN PROGRAM

# change the input from indented spaces to
# level_number<space>text
(
    #subshell - change IFS
    IFS=,
    while read -r spaces content
    do
        echo $(( (${#spaces} / 2) + 1 )) "$content"
    done < <(sed 's/[^ ]/,&/' < "$input")

) | (   #pipe to another subshell
    checkline 0 #recurse by levels
)

Sry for long code - 有人可以帮忙吗?

2 个答案:

答案 0 :(得分:5)

有趣的问题。

这个awk(可能是一行代码)命令完成了这项工作:

awk -F'  ' 'NF<=p{for(i=1;i<=p;i++)printf "%s%s", a[i],(i==p?RS:"/")
            if(NF<p)for(i=NF;i<=p;i++) delete a[i]}
            {a[NF] =$NF;p=NF }
            END{for(i=1;i<=NF;i++)printf "%s%s", a[i],(i==NF?RS:"/")}' file

您可以看到上面有重复的代码,如果您愿意,可以将它们提取到函数中。

使用您的数据进行测试:

kent$  cat f
a1
  b1
    c1
    c2
    c3
  b2
    c1
      d1
      d2
  b3
  b4
a2
a3
  b1
  b2
    c1
    c2

kent$  awk -F'  ' 'NF<=p{for(i=1;i<=p;i++)printf "%s%s", a[i],(i==p?RS:"/")
if(NF<p)for(i=NF;i<=p;i++) delete a[i]}
{a[NF] =$NF;p=NF }END{for(i=1;i<=NF;i++)printf "%s%s", a[i],(i==NF?RS:"/")} ' f
a1/b1/c1
a1/b1/c2
a1/b1/c3
a1/b2/c1/d1
a1/b2/c1/d2
a1/b3
a1/b4
a2
a3/b1
a3/b2/c1
a3/b2/c2    

答案 1 :(得分:4)

我最近不得不做类似的事情,通过一些调整,我可以在这里发布我的脚本:

#!/bin/bash

prev_level=-1
# Index into node array
i=0

# Regex to screen-scrape all nodes
tc_re="^((  )*)(.*)$"
while IFS= read -r ln; do
    if  [[ $ln =~ $tc_re ]]; then
        # folder level indicated by spaces in preceding node name
        spaces=${#BASH_REMATCH[1]}
        # 2 space characters per level
        level=$(($spaces / 2))
        # Name of the folder or node
        node=${BASH_REMATCH[3]}        
        # get the rest of the node path from the previous entry
        curpath=( ${curpath[@]:0:$level} $node )

        # increment i only if the current level is <= the level of the previous
        # entry
        if [ $level -le $prev_level ]; then
            ((i++))
        fi

        # add this entry (overwrite previous if $i was not incremented)
        tc[$i]="${curpath[@]}"

        # save level for next iteration
        prev_level=$level
    fi
done

for p in "${tc[@]}"; do
    echo "${p// //}"
done

输入来自STDIN,因此您必须执行以下操作:

$ ./tree2path.sh < ifile.tree 
a1/b1/c1
a1/b1/c2
a1/b1/c3
a1/b2/c1/d1
a1/b2/c1/d2
a1/b3
a1/b4
a2
a3/b1
a3/b2/c1
a3/b2/c2
$