bash 从某一行开始对文件进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1278335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:08:05  来源:igfitidea点击:

Sort file beginning at a certain line

linuxbashsorting

提问by user148813

I'd like to be able to sort a file but only at a certain line and below. From the manual sort isn't able to parse content so I'll need a second utility to do this. read? or awk possibly? Here's the file I'd like to be able to sort:

我希望能够对文件进行排序,但只能在某一行及以下进行排序。手动排序无法解析内容,因此我需要第二个实用程序来执行此操作。读?或 awk 可能吗?这是我希望能够排序的文件:

tar --exclude-from=$EXCLUDE_FILE --exclude=$BACKDEST/$PC-* \
-cvpzf $BACKDEST/$BACKUPNAME.tar.gz \
/etc/X11/xorg.conf \
/etc/X11/xorg.conf.1 \
/etc/fonts/conf.avail.1 \
/etc/fonts/conf.avail/60-liberation.conf \

So for this case, I'd like to begin sorting on line three. I'm thinking I'm going to have to do a function to be able to do this something like

所以对于这种情况,我想从第三行开始排序。我想我将不得不做一个功能才能做到这一点

cat backup.sh | while read LINE; do echo $LINE | sort; done

Pretty new to this and the script looks like it's missing something. Also, not sure how to begin at a certain line number.

对此很新,脚本看起来好像缺少一些东西。另外,不确定如何从某个行号开始。

Any ideas?

有任何想法吗?

回答by retracile

Something like this?

像这样的东西?

(head -n 2 backup.sh; tail -n +3 backup.sh | sort) > backup-sorted.sh

You may have to fixup the last line of the input... it probably doesn't have the trailing \ for the line continuation, so you might have a broken 'backup-sorted.sh' if you just do the above.

您可能需要修复输入的最后一行......它可能没有行延续的尾随 \,因此如果您只是执行上述操作,则可能会损坏 'backup-sorted.sh'。

You might want to consider using tar's --files-from (or -T) option, and having the sorted list of files in a data file instead of the script itself.

您可能需要考虑使用 tar 的 --files-from(或 -T)选项,并在数据文件中而不是脚本本身中使用已排序的文件列表。

回答by Steve B.

clumsy way:

笨方法:

len=$(cat FILE | wc -l)
sortable_len=$((len-3))

head -3 FILE > OUT
tail -$sortable_len FILE | sort >> OUT

I'm sure someone will post an elegant 1-liner shortly.

我相信很快就会有人发布优雅的 1-liner。

回答by Vigneswaran R

Sort the lines excluding the (2 lines) header, just for view.

对不包括(2 行)标题的行进行排序,仅供查看。

cat file.txt | awk '{if (NR < 3) print 
cat file.txt | awk '{if (NR < 3) print 
cat file.txt | awk '{if (NR < 3) print 
(read line; echo "$line"; sort) < file.txt
> "file_sorted.txt"; else print
file.txt | (read line; echo "$line"; sort)
}' | sort >> file_sorted.txt
> "/dev/stderr"; else print
(read line; echo "$line"; read line; echo "$line"; sort) < file.txt
}' 2> file_sorted.txt | sort >> file_sorted.txt
> "/dev/stderr"; else print
awk '{ if ( NR > 2 ) { print ##代码## } }' file.txt | sort
}' | sort

Sort the lines excluding the (2 lines) headers and send the output to another file.

对不包括(2 行)标题的行进行排序,并将输出发送到另一个文件。

Method #1:

方法#1:

##代码##

Method #2:

方法#2:

##代码##

回答by sffortytwo

You could try this:

你可以试试这个:

##代码##

It takes one line and echoes it, then sorts the rest. You can also:

它需要一行并回应它,然后对其余的进行排序。你也可以:

##代码##

For two lines, just repeat the read and echo:

对于两行,只需重复读取和回显:

##代码##

回答by Al.

Using awk:

使用awk

##代码##

NR is a built-in awkvariable and contains the current record/line number. It starts at 1.

NR 是一个内置awk变量,包含当前记录/行号。它从 1 开始。