连接多个无标题文件

连接多个无标题文件

我有几个目录(“amazon”、“niger”……),其中有几个子目录(“gfdl”、“hadgem”……),其中还有几个子目录(“rcp8p5”、“rcp4p5”……)。在最后的子目录中,我总是有两个文件夹(“historical”、“projected”),其中包含数千个具有相同框架的表。因此,我想连接这些表(存在于最后子目录的两个文件夹中),以便每次连接表时都只有一个只有一个标题的大表,而不是标题。有人知道怎么做吗?

我目前正在使用以下循环结构:

#!/bin/bash
# usage:cat_dat dirname

data_dir=/scratch/01/stevens/climate_scenario/river

for river in tagus
  do
   for gcm in gfdl-esm2m hadgem2-es
     do
      for scenario in rcp8p5 rcp4p5 rcp6p0 rcp2p6
        do
          find "${data_dir}/${river}/${gcm}/${scenario}" name \*.dat -exec cat {} + >> "${data_dir}/${river}/${gcm}/${scenario}.dat"
      done
   done
done

但我无法用这个删除标题!任何帮助我都非常感谢!谢谢!

答案1

awk在单个文件夹中使用

awk 'NR==1 {header=$_} FNR==1 && NR!=1 { $_ ~ $header getline; } {print}' *.dat > out

find如果awk 您需要当前文件夹及其子文件夹中的所有文件。您可以将其替换.为所需的文件夹。

find . -type f -name "*.dat" -print0 | \
    xargs -0 awk 'NR==1 {header=$_} FNR==1 && NR!=1 { $_ ~ $header getline; } {print}' > out

或者getline 不好(谢谢@fedorqui

find . -type f -name "*.dat" -exec awk 'NR==1 || FNR!=1' {} + ;

例子

% cat foo1.dat 
a   b   c
1   2   3

% cat foo2.dat
a   b   c
4   5   6

% awk 'NR==1 {header=$_} FNR==1 && NR!=1 { $_ ~ $header getline; } {print}' *.dat > out

% cat out 
a   b   c
1   2   3
4   5   6

答案2

您可以使用while通过find流程替代

d=0
while IFS= read -r file
do
   [ "$d" -ge 1 ] && tail -n +2 "$file" || cat "$file"
   (( d ++ ))
done < <(find "/dir/folder" name *.dat)

因此它将cat对第一场匹配和tail -n +2其余匹配执行。


或者,如果您将所有文件放在同一个目录中,您可以说:

awk 'FNR>1 || NR==1' files*

FNR==1这将匹配除和之外的所有内容NR>1,即除第一个文件之后的文件头之外的所有内容。为什么?因为NR保存的是总体读取的行数,而FNR保存的是当前正在读取的文件的行数。

相关内容