지정된 행 수에 따라 CSV 파일을 분할하는 방법은 무엇입니까?
LINUX 서버에 CSV 파일 (약 10,000 행, 각 행마다 300 열)이 저장되어 있습니다. 이 CSV 파일을 각각 20 개 레코드의 500 개 CSV 파일로 나누고 싶습니다. (각각 원본 CSV에있는 것과 동일한 CSV 헤더를 가짐)
이 변환을 도와주는 Linux 명령이 있습니까?
그것을 함수로 만들었습니다. 이제 전화 할 수 있습니다.splitCsv <Filename> [chunkSize]
splitCsv() {
HEADER=$(head -1 $1)
if [ -n "$2" ]; then
CHUNK=$2
else
CHUNK=1000
fi
tail -n +2 $1 | split -l $CHUNK - $1_split_
for i in $1_split_*; do
echo -e "$HEADER\n$(cat $i)" > $i
done
}
위치 : http://edmondscommerce.github.io/linux/linux-split-file-eg-csv-and-keep-header-row.html
Linux split 명령을 사용하십시오.
split -l 20 file.txt new
"file.txt"파일을 각각 20 줄의 텍스트를 포함하는 "new"이름으로 시작하는 파일로 분할합니다.
man split
자세한 내용은 Unix 프롬프트에서 입력 하십시오. 그러나 먼저 file.txt에서 헤더를 제거한 다음 ( tail
예 : 명령 사용 ) 각 분할 파일에 다시 추가해야합니다.
그러면 모든 파일이 Part1-Part500으로 표시됩니다.
#!/bin/bash
FILENAME=10000.csv
HDR=$(head -1 $FILENAME) # Pick up CSV header line to apply to each file
split -l 20 $FILENAME xyz # Split the file into chunks of 20 lines each
n=1
for f in xyz* # Go through all newly created chunks
do
echo $HDR > Part${n} # Write out header to new file called "Part(n)"
cat $f >> Part${n} # Add in the 20 lines from the "split" command
rm $f # Remove temporary file
((n++)) # Increment name of output part
done
이것은 작동합니다 !!!
file_name
= 분할하려는 파일의 이름.
10000
= 각 분할 파일에 포함될 행 수
file_part_
= 분할 파일 이름의 접두사 (file_part_0, file_part_1, file_part_2..etc 계속됨)
split -d -l 10000 file_name.csv file_part_
참고 URL : https://stackoverflow.com/questions/20721120/how-to-split-csv-files-as-per-number-of-rows-specified
'IT박스' 카테고리의 다른 글
JavaScript : 2 일 전 날짜를 계산하는 방법은 무엇입니까? (0) | 2020.10.29 |
---|---|
자바 스크립트 캔버스로 이미지 크기 조정 (부드럽게) (0) | 2020.10.29 |
angular-cli 서버-API 요청을 다른 서버로 프록시하는 방법은 무엇입니까? (0) | 2020.10.28 |
iPhone에서 빈 영역을 터치하면 키보드를 숨기는 방법 (0) | 2020.10.28 |
Java에서 문자열 부분 제거 (0) | 2020.10.28 |