解析 space 分隔文件并在 bash 中执行操作

Parsing a space delimited file and performing operations in bash

我正在尝试读取 bash 中 space 分隔的基本文件,我想对变量执行操作。

在 bash 中引用某些 "columns" 的术语是什么? 我正在尝试明确使用 bash。如果有有用的文档专门提到如何分隔文件和执行基本操作,那将非常有用。

我的文本文档示例如下:

123456789 LastName FirstName 1 2 3
123456789 LastName FirstName 1 2 3
123456789 LastName FirstName 1 2 3
123456789 LastName FirstName 1 2 3
123456789 LastName FirstName 1 2 3

我想对它进行排序并在多个列上执行操作。

我已经使用 awk 完成了此操作,但我想在 bash 中完成此操作。 我的 awk 实现:

awk '{average = ( +  + )/3} {print (average, "[""]", ",", ); average = 0}' $'readme.txt'

如何实现?

您需要 sort and cut 分别进行排序和拆分。

cut --delimiter=' ' --fields=LIST 其中 LIST 是逗号分隔的列索引列表 returns 每个 space-split 行的部分由 LIST 中的索引表示。

sort --field-separator=' ' --keys=POS 对文件的行进行排序并将它们输出到标准输出。 --field-separator=' ' 导致位置由空格分隔。

POS is F[.C][OPTS], where F is the field number and C the character position in the field; both are origin 1. If neither -t nor -b is in effect, characters in a field are counted from the beginning of the preceding whitespace. OPTS is one or more single-letter ordering options, which override global ordering options for that key. If no key is given, use the entire line as the key.

您可以使用 expr and bc 进行数学计算。

wc -l 会给你总行数。

如果您有 headers 需要忽略,请使用 tail -n +2 让整个文件从第二行开始。

将所有东西与管道和子壳捆绑在一起。一般来说,您想要的处理方式就是 awk 占有一席之地的原因。