파일을 줄 수로 분할합니다. 각 줄에는 헤더가 포함됩니다.

파일을 줄 수로 분할합니다. 각 줄에는 헤더가 포함됩니다.

.txt파일을 헤더를 포함하여 각각 100줄씩 더 작은 파일로 분할 해야 합니다 . 이것이 관련이 있는지는 모르겠지만 원본 파일은 다음과 같이 구분되었습니다.

COLUMN1 | COLUMN2 | COLUMN3
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9

헤더 행을 가지려면 이 분할에서 생성된 각 파일이 필요합니다. 또한 다른 디렉터리에서 생성/이동해야 하며 등과 같은 이름 패턴을 따라야 합니다 file_01.txt.file_02.txt

답변1

awk 'NR==1        {a=$0}
    (NR-1)%100==0 {print a > "d/file_" int(1+(NR-1)/100)}
                  {print   > "d/file_" int(1+(NR-1)/100)}' 

답변2

gnu split제목을 변수에 저장한 다음 split2번째 줄부터 옵션을 사용하여 제목을 먼저 쓴 다음 각 섹션의 99줄을 쓰고 출력 디렉터리를 지정할 수 --filter있습니다(예: path to/output dir/).

header=$(head -n 1 infile.txt)
export header
tail -n +2 infile.txt | split -l 99 -d --additional-suffix=.txt \
--filter='{ printf %s\\n "$header"; cat; } >path\ to/output\ dir/$FILE' - file_

그러면 아래와 같이 100줄의 스니펫이 생성됩니다.

path to/output dir/file_01.txt
path to/output dir/file_02.txt
path to/output dir/file_03.txt
..............................

답변3

bash에서 나를 위해 일했습니다.

lines=100; { read header && sed "1~$((${lines}-1)) s/^/${header}\n/g" | split -l $lines --numeric-suffixes=1 --additional-suffix=.txt - file_ ; } < inputfile.txt

관련 정보