在Linux中将文件排序为注释
假设我有一个sort_me.txt文件:在Linux中将文件排序为注释,linux,sorting,Linux,Sorting,假设我有一个sort_me.txt文件: a d b c f g // dont mix the two sections a c d b 目前,我执行了明显的排序\u me.txt,得到: a a b b c c d d // dont mix the two sections f g 这当然不是我想要的,我想要的是它对评论之前的部分和评论之后的部分分别进行排序 预期结果如下: a b c d f g // dont mix the two sections a b c d 救援
a
d
b
c
f
g
// dont mix the two sections
a
c
d
b
目前,我执行了明显的排序\u me.txt
,得到:
a
a
b
b
c
c
d
d
// dont mix the two sections
f
g
这当然不是我想要的,我想要的是它对评论之前的部分和评论之后的部分分别进行排序
预期结果如下:
a
b
c
d
f
g
// dont mix the two sections
a
b
c
d
救援人员:
perl -007 -nE '
@sections = map [ split /\n/ ], split m{^(?=//)}m;
say join "\n", sort @$_ for @sections;
' -- file
读取整个文件,而不是逐行处理(仅当文件不是很大时才有效)-007
是一个数组数组,外部数组对应于节,内部数组对应于单独的行@sections
perl -ne '
sub out { print sort @lines; @lines = $_ }
if (m{^//}) { out() }
else { push @lines, $_ }
END { out() }
' -- file
如果没有perl,您可以使用如下脚本:
#!/bin/bash
FILE_NAME=$1
SEPARATOR='//'
LINE_NUMBER=`grep -n $SEPARATOR $FILE_NAME | cut -f1 -d:`
FILE_LENGTH=`wc -l $FILE_NAME | cut -f1 -d\s`
head -$(($LINE_NUMBER-1)) $FILE_NAME | sort
grep $SEPARATOR $FILE_NAME
tail -$(($FILE_LENGTH-$LINE_NUMBER-1)) $FILE_NAME | sort
它搜索分隔线并逐个对部分进行排序。当然,如果您有两个以上的分区,它将不起作用。我正在考虑使用
csplit
将分区拆分为单独的文件,但当然应该有更简单的方法来实现这一点:
#!/bin/bash
linenum=`csplit -z $1 /^$/ {*}`
count=0
output=''
for line in $linenum
do
file=`printf "xx%.2d" $count`
sorted=`cat $file | sort`
output="$output$sorted"
((count++))
done
echo "$output"
请注意,csplit
将为每个部分创建一个临时文件,因此您可以更新上述脚本以取消其中每个部分的链接,即unlink$file