Bashの複雑なテキストの並べ替え

Bashの複雑なテキストの並べ替え

次の入力が与えられた場合:

# Lines starting with # stay the same
# Empty lines stay the same
# only lines with comments should change

ls  # show all major directories
              # and other things

cd      # The cd command - change directory  
            # will allow the user to change between file directories

touch             # The touch command, the make file command 
                # allows users to make files using the Linux CLI #  example, cd ~

bar foo baz # foo foo foo

#コメントで始まる行とコメントを含まない行を維持し、他のすべてのコメントを同じ列に並べ替える必要があります。

希望の出力:

# Lines starting with # stay the same
# Empty lines stay the same
# Only lines with # in middle should change and be aligned

ls              # show all major directories
                # and other things

cd              # The cd command - change directory  
                # will allow the user to change between file directories

touch           # The touch command, the make file command 
                # allows users to make files using the Linux CLI #  exmaple, cd ~

bar foo baz     # foo foo foo

私が今まで持っているもの:

# Building an array out of input
 while IFS=$'\n' read -r; do 
    lines+=("$REPLY") 
 done 

# Looping through array and selecting elemnts that need change 
for i in "${lines[@]}"
  do
    if  [[ ${i:0:1} == ';' || $i != *";"* ]];
      then
        echo "DOESNT CHANGE: #### $i"
    else 
        echo "HAS TO CHANGE: #### $i"
        array+=( "${i%%";"*}" );
        array2+=("${i##";"}")
    fi
done

# Trying to find the longest line to decide how much space I need to add for each element
max = ${array[0]}

for n in "${array[@]}" ; do
    ((${#n} > max)) && max=${#n}
    echo  "Length:" ${#n} ${n}
done

#Longest line
echo $max

# Loop for populating array 
for j in "${!array2[@]}" ; do
    echo "${array2[j]} " | sed -e "s/;/$(echo "-%20s ;") /g" 
done

私はあまりにも多くのことをしていると感じました。この問題を解決するより簡単な方法があるべきだと思います。

ベストアンサー1

すべてのコマンドと引数に、および他の#文字(バイト1として提供されるASCII文字など)が含まれていない場合は、その文字を追加の区切り文字として挿入してcolumnコメントを並べ替えることができます(参照:この回答)。したがって、次のようになります。

$ sed $'s/#/\001#/' input-file | column -ets $'\001'
# Lines starting with # stay the same
# Empty lines stay the same
# only lines with comments should change

ls                                        # show all major directories
                                          # and other things

cd                                        # The cd command - change directory
                                          # will allow the user to change between file directories

touch                                     # The touch command, the make file command
                                          # allows users to make files using the Linux CLI #  example, cd ~

bar foo baz                               # foo foo foo

column空白行の削除防止をサポートしていない場合は、-e空白行に何かを追加できます(たとえば、上記で使用したスペースや区切り文字など)。

$ sed $'s/#/\001#/;s/^$/\001/' input-file | column -ts $'\001'
# Lines starting with # stay the same
# Empty lines stay the same
# only lines with comments should change

ls                                        # show all major directories
                                          # and other things

cd                                        # The cd command - change directory
                                          # will allow the user to change between file directories

touch                                     # The touch command, the make file command
                                          # allows users to make files using the Linux CLI #  example, cd ~

bar foo baz                               # foo foo foo

おすすめ記事