
파일에는 다음과 같은 줄이 포함되어 있습니다.
acb/xyz/row<t>
acb/xyz/row<t>
abc/xyz/row<1>
abc/xyz/row<1>
abc/xyz/row<0>
abc/xyz/row<0>
abc/xyz/row<3>
abc/xyz/row<3>
abc/xyz/row<2>
abc/xyz/row<2>
abc/xyz/row<4>
abc/xyz/row<4>
abc/xyz/row<b>
abc/xyz/row<b>
그래서 나는 출력을 원한다.
acb/xyz/row<t>
acb/xyz/row<t>
abc/xyz/row<b>
abc/xyz/row<b>
abc/xyz/row<0>
abc/xyz/row<0>
abc/xyz/row<1>
abc/xyz/row<1>
abc/xyz/row<2>
abc/xyz/row<2>
abc/xyz/row<3>
abc/xyz/row<3>
abc/xyz/row<4>
abc/xyz/row<4>
t(상단) 및 b(하단)가 숫자 앞에 오며 해당 순서로 정렬됩니다.
답변1
언제든지 Decor-sort-un장식 방법을 사용하여 -2를 t
, -1
에 할당할 수 있습니다 b
.
<your-file awk -F'[<>]' '
{print $2 == "t" ? -2 : $2 == "b" ? -1 : $2, $0}' |
sort -n |
cut -d' ' -f2-
답변2
다른 방법이 있을 수도 있지만 이를 수행하는 가장 좋은 방법은 여러 명령을 사용하는 것이며 이를 수행하려면 작은 스크립트를 사용할 수 있습니다.
여기의 스크립트는 먼저 가 있는 모든 행을 가져온 <t>
다음 가 있는 모든 행을 가져온 <b>
다음 숫자가 있는 모든 행을 가져와 <#>
숫자별로 정렬합니다.
생략할 수 있는 | sort
경우오직내부 내용 <...>
:
#!/usr/bin/env bash
# get all rows with <t>
grep '<t>' input.txt | sort > output.txt
# get all rows with <b>, append to result
grep '<b>' input.txt | sort >> output.txt
# find lines with numbers inside <#>, pull the
# number out to start, sort numerically, then remove
# numbers pulled out to start; append to result
sed -n 's/.*<\([0-9]\+\)>.*/\1 &/p' input.txt | \
sort -n | sed 's/^[0-9]\+ //' >> output.txt
입력 파일은 input.txt
이고 출력은 output.txt
필요에 따라 변경된다고 가정합니다.
스크립트를 실행 가능하게 만들고 chmod a+x script.sh
실행하십시오 ./script.sh
.