Leaning lines (tree) in a way similar to lines

I have input files with a structure like the following:

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

Each level is indented by 2 spaces. Required Output:

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

This is similar to the file system if the next line has a larger indent, the current line is like a β€œdirectory”, and when it has the same indent, it looks like a β€œfile”. You need to print the full paths of the "files".

Trying to solve this without any high-level language, for example python, perlonly with basic bash commands.

My current code / idea is based on a recursive function call and working with glass, but the problem is with the "logic". The code currently displays the following:

a1 b1 c1
a1 b1
a1

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

only the 1st row is ok - so recursion processing is wrong ...

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 - can anyone help?

+4
2

.

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    
+5

- , script:

#!/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
$ 
+4

Source: https://habr.com/ru/post/1533431/


All Articles