Ваша ссылка указывает на /folder2. Ваш pwd -это /folder1/linkToFolder2, то есть то же самое, что и /folder2/, и 'ls ..' во всех случаях должен показывать вам вышеуказанный каталог, независимо от того, находитесь ли вы в /folder1 или /folder2
Если вам разрешено использовать злые внешние приспособления, такие как сортировка
и вырезание
:
#! /bin/bash
while IFS= read -r line; do
squeezed=$( tr -d '[:blank:]' <<<"$line" )
printf '%d\t%d\t%s\n' ${#line} ${#squeezed} "$line"
done | sort -n -k 1 -k 2 | cut -f 3-
Редактировать: Так как все это делают , вот решение с perl
:
perl -e 'print sort { length $a <=> length $b || $a =~ y/ \t//c <=> $b =~ y/ \t//c } <>'
sortByLength ()
{
local -a sorted=() sort2
local line sline sline2 pointer
while IFS= read -r line; do
sorted[${#line}]+="$line"
done
for pointer in ${!sorted[@]}
do
# ((pointer)) || echo 0: # This will trace empty lines
sort2=()
line="${sorted[pointer]}"
while [ "$line" ]; do
sline=${line:0:pointer}
line=${line:pointer}
sline2=${sline// }
sort2[${#sline2}]+=${sline}$'\n'
done
# echo $pointer: # This will trace lines length
printf "%s" "${sort2[@]}"
done
}
Это может быть намного быстрее, так как нет форков!
Функция:
sortlen() { while read x ; do \
y=`tr -d '[:blank:]' <<< "$x"` ; echo ${#x} ${#y} "$x" ; \
done | sort -k 1g,2 -k 2g,3 | cut -d' ' -f3-; }
Тест:
printf "a b c\nabcde\nabcdefg\na\nabcd\n" | sortlen
Вывод:
a
abcd
a b c
abcde
abcdefg
Используя те же принципы, что и другие (получить длину строки, с пробелами и без них, отсортировать их, а затем удалить), но с awk
:
awk '{NC = length(gensub(/[[:space:]]/, "", "g")); print length, NC, $0}' file |
sort -nk1,2 |
sed -r 's/^([0-9]+ ){2}//'
gensub(/[[:space:]]/, "", "g")
удаляет все пробельные символы в строке, после чего мы получаем длину оставшейся строки
Используя текст вопроса вверх в кодовый блок, свернутый до ширины 80 символов:
$ awk '{NC = length(gensub(/[[:space:]]/, "", "g")); print length, NC, $0}' foo | sort -nk1,2 | sed -r 's/^([0-9]+ ){2}//'
increasing order).
Here's what I've got so far:
f the idiosyncrasies of bash.
iven a bunch of lines from STDIN, sort them first by the length of the line in i
I've tried this a couple of different ways but I usually get caught up in some o
, sort them by the number of nonblank characters contained in the lines (also in
I am working on learning bash scripting but I am struggling with this problem. G
ncreasing order. Then, if there are any lines with the same number of characters
Не удержался и добавил sed
решение:
sed 'h;s/.*/0:0;0123456789+/;G;:count
s/\(.\)\(;.*\1\)\(.\)\(.*\n\)[^[:space:]]/\3\2\3\4x/
s/\(.\)\(:.*\1\)\(.\)\(.*\n\)./\3\2\3\4/;:overflow
s/^+/10/;s/:+/:10/;s/\(.\)+\(.*\1\)\(.\)\(.*\n\)/\30\2\3\4/;t overflow
/\n./b count
G;s/;.*\n/:/' file|sort -t: -n -k 1 -k 2|cut -d: -f 3-
Скрипт sed
подсчитывает символы и непробелы и помещает их в начало строки, sort
и cut
идут прямо вперед. И пожалуйста, не говорите мне, что это чепуха. Для меня это весело.(-: