使用 awk 函数从 hdfs 目录中提取特定部分
extract specific parts from hdfs directory using awk function
我正在尝试从 /rec/flux_entrant/archive/le501/tble91_formation_eligible/*
目录中提取特定部分。该目录位于 HDFS
中,因此我们可以使用以下命令公开其包含的内容:
hdfs dfs -ls /rec/flux_entrant/archive/le501/tble91_formation_eligible/*
return
/rec/flux_entrant/archive/le501/tble91_formation_eligible/20220104-221755/00000.deflate
/rec/flux_entrant/archive/le501/tble91_formation_eligible/20220103-231754/00001.deflate
/rec/flux_entrant/archive/le501/tble91_formation_eligible/20220111-152145/00002.deflate
/rec/flux_entrant/archive/le501/tble91_formation_eligible/20220112-155012/00003.deflate
我的目标是仅提取由 ( not xxx.deflate 给出的这些路径的最后一部分文件) :
20220104-221755
、20220103-231754
、20220111-152145
和 20220112-155012
然后按日期 => 20220110
过滤,这样,最终结果应该是:
20220111-152145
和 20220112-155012
因为 20220111
和 20220112
=> 到 20220110
我尝试使用 awk
命令使用命令 :
hdfs dfs -ls /rec/flux_entrant/archive/le501/tble91_formation_eligible/* | awk -F'/' '{split($NF, a, "-"); if (a[1]>20220110) print $NF}'
但是这个return:00003.deflate
和00002.deflate
而不是20220111-152145
和20220112-155012
编辑
按照@Tom的建议,我用print $(NF-1)
代替了$NF
,但是过滤效果不好。我还尝试在列表变量中获取结果:
OUTPUT=$(hdfs dfs -ls /rec/flux_entrant/archive/le501/tble91_formation_eligible/* |
awk -F'/' '{split($NF, a, "-"); if (a[1]>=20220110) print $(NF-1)}')
echo ${OUTPUT}
给予
Found 5 items 20200916-170926 20200916-170926 20200916-170926 20200916-170926 20200916-170926 Found 5 items 20200916-182251
这不好,因为 20200916
、20200916
... 不是 => 20220110
我还需要从最终结果
中删除Found 5 items
有什么帮助吗?谢谢
试试这个,使用 AWK 的变量 FPAT
:
hdfs dfs -ls /rec/flux_entrant/archive/le501/tble91_formation_eligible/* |
awk -v startdate="20220110" 'BEGIN{FPAT="[0-9]{8}-[0-9]{6}"}( >= startdate){print }'
我使用变量 startdate
来避免将字符串 20220110
硬编码到 AWK 代码中。
解释:FPAT
是一个正则表达式,它描述了 AWK 必须考虑的字段:在我们的例子中,是一个 8 位数字的序列,后跟一个连字符和 6 位数字。 AWK 使用指令 print
打印它在输入的每一行中找到的唯一序列,条件是 ( >= startdate)
.
据我了解,您实际上想要这样的东西从:
$ hdfs dfs ls -d /path/to/dir/*/
这将 select /path/to/dir
下的所有子目录,并且由于标志 -d
而不会遍历它们(参见 hadoop documentation。从那时起,很容易使目录 selection。目录的形式是 YYYYMMDD-hhmmss
,因此可以按字典顺序排序。所以你可以这样做:
$ hdfs dfs ls -d /path/to/dir/*/ | awk -F/ '($NF<"20220128"){print $NF}'
请注意,我们在 $NF<"20220128"
中进行字符串比较而不是数值比较。由于 awk 的内部结构,您可以进行数字比较,并且 awk 会在转换过程中去除字符串的所有 non-numeric 部分。所以你可以这样做:
$ hdfs dfs ls -d /path/to/dir/*/ | awk -F/ '($NF+0<20220128){print $NF}'
我正在尝试从 /rec/flux_entrant/archive/le501/tble91_formation_eligible/*
目录中提取特定部分。该目录位于 HDFS
中,因此我们可以使用以下命令公开其包含的内容:
hdfs dfs -ls /rec/flux_entrant/archive/le501/tble91_formation_eligible/*
return
/rec/flux_entrant/archive/le501/tble91_formation_eligible/20220104-221755/00000.deflate
/rec/flux_entrant/archive/le501/tble91_formation_eligible/20220103-231754/00001.deflate
/rec/flux_entrant/archive/le501/tble91_formation_eligible/20220111-152145/00002.deflate
/rec/flux_entrant/archive/le501/tble91_formation_eligible/20220112-155012/00003.deflate
我的目标是仅提取由 ( not xxx.deflate 给出的这些路径的最后一部分文件) :
20220104-221755
、20220103-231754
、20220111-152145
和 20220112-155012
然后按日期 => 20220110
过滤,这样,最终结果应该是:
20220111-152145
和 20220112-155012
因为 20220111
和 20220112
=> 到 20220110
我尝试使用 awk
命令使用命令 :
hdfs dfs -ls /rec/flux_entrant/archive/le501/tble91_formation_eligible/* | awk -F'/' '{split($NF, a, "-"); if (a[1]>20220110) print $NF}'
但是这个return:00003.deflate
和00002.deflate
而不是20220111-152145
和20220112-155012
编辑
按照@Tom的建议,我用print $(NF-1)
代替了$NF
,但是过滤效果不好。我还尝试在列表变量中获取结果:
OUTPUT=$(hdfs dfs -ls /rec/flux_entrant/archive/le501/tble91_formation_eligible/* |
awk -F'/' '{split($NF, a, "-"); if (a[1]>=20220110) print $(NF-1)}')
echo ${OUTPUT}
给予
Found 5 items 20200916-170926 20200916-170926 20200916-170926 20200916-170926 20200916-170926 Found 5 items 20200916-182251
这不好,因为 20200916
、20200916
... 不是 => 20220110
我还需要从最终结果
Found 5 items
有什么帮助吗?谢谢
试试这个,使用 AWK 的变量 FPAT
:
hdfs dfs -ls /rec/flux_entrant/archive/le501/tble91_formation_eligible/* |
awk -v startdate="20220110" 'BEGIN{FPAT="[0-9]{8}-[0-9]{6}"}( >= startdate){print }'
我使用变量 startdate
来避免将字符串 20220110
硬编码到 AWK 代码中。
解释:FPAT
是一个正则表达式,它描述了 AWK 必须考虑的字段:在我们的例子中,是一个 8 位数字的序列,后跟一个连字符和 6 位数字。 AWK 使用指令 print
打印它在输入的每一行中找到的唯一序列,条件是 ( >= startdate)
.
据我了解,您实际上想要这样的东西从:
$ hdfs dfs ls -d /path/to/dir/*/
这将 select /path/to/dir
下的所有子目录,并且由于标志 -d
而不会遍历它们(参见 hadoop documentation。从那时起,很容易使目录 selection。目录的形式是 YYYYMMDD-hhmmss
,因此可以按字典顺序排序。所以你可以这样做:
$ hdfs dfs ls -d /path/to/dir/*/ | awk -F/ '($NF<"20220128"){print $NF}'
请注意,我们在 $NF<"20220128"
中进行字符串比较而不是数值比较。由于 awk 的内部结构,您可以进行数字比较,并且 awk 会在转换过程中去除字符串的所有 non-numeric 部分。所以你可以这样做:
$ hdfs dfs ls -d /path/to/dir/*/ | awk -F/ '($NF+0<20220128){print $NF}'