ID와 이메일이라는 2 개의 필드가있는 하나의 CSV 파일이 있다고 가정합니다. 이메일과 이름의 2 개 필드가있는 다른 파일이 있습니다. 이메일에 세 필드가 모두 결합 된 파일을 어떻게 생성 할 수 있습니까?
개정 :
이메일의 두 목록을 알파벳순으로 정렬 한 다음 가입해야합니다. 이메일 필드가 file1의 두 번째 필드와 file2의 첫 번째 필드 인 경우 :
sort -t , -k 2,2 file1.csv > sort1.csv
sort -t , -k 1,1 file2.csv > sort2.csv
join -t , -1 2 -2 1 sort1.csv sort2.csv > sort3.csv
매개 변수 의미
-t, : ','는 필드 구분자 -k 2,2 : 두 번째 필드에서 문자 정렬 -k 1,1 : 첫 번째 필드에서 문자 정렬 -1 2 : 파일 1, 두 번째 필드 -2 1 : 파일 2, 첫 번째 필드 > : 파일로 출력
생산하다
이메일, ID, 이름 이메일, ID, 이름 ...
이메일로 알파벳순으로 정렬됩니다.
두 파일 중 하나에서 누락 된 이메일이 있으면 결과에서 생략됩니다.
csvkit 사용 :
csvjoin -c email id_email.csv email_name.csv
또는
csvjoin -c 2,1 id_email.csv email_name.csv
과도하지만, 두 종류의 테이블로 데이터베이스 (예 : OpenOffice Base)로 가져 와서 원하는 출력 인 보고서를 정의 할 수 있습니다.
CSV 가져 오기에 문제가있는 경우 스프레드 시트 프로그램 (예 : OpenOffice Calc)에서 가져올 수 있습니다. 결과는 데이터베이스로 쉽게 전송할 수 있습니다.
향후 참조로 AWK 를 사용하여 작업을 시작할 수 있습니다. 모든 * nix 시스템에 어떤 형태로든 존재하는 아주 간단한 작은 스크립팅 언어이며 유일한 임무는 표준으로 구분 된 텍스트 데이터베이스를 조작하는 것입니다. 몇 줄의 일회용 스크립트로 매우 유용한 작업을 수행 할 수 있습니다. 언어는 작고 우아하며 내가 아는 다른 것보다 더 나은 유틸리티/복잡성 비율을 가지고 있습니다.
CSV Cruncher 를 시도하십시오.
CSV 파일을 SQL 테이블로 사용하고 SQL 쿼리를 허용하여 다른 CSV 또는 JSON 파일을 생성합니다.
귀하의 경우에는 다음으로 전화하십시오.
crunch -in tableA.csv tableB.csv -out output.csv \
"SELECT tableA.id, tableA.email, tableB.name
FROM tableA LEFT JOIN tableB USING (email)"
도구에는 Java 8 이상이 필요합니다.
몇 가지 장점 :
join
기반 솔루션보다 사용하고 이해하기 쉽습니다.면책 조항 : 그 도구를 작성했습니다. 구글 코드가 종료 된 후 혼란 스러웠지만, 사용하면서 되살아나고 새로운 기능을 추가했습니다.
Bash 5.0.3에서 GNU Coreutils 8.30 및 hyperslug의 대답 을 기반으로 구축) :
중복 된 행이있는 정렬되지 않은 CSV 파일이 있고 file1.csv
또는 file2.csv
행에서 누락 된 필드로 인해 데이터를 생략하지 않으려면 다음을 수행 할 수 있습니다.
파일 1을 필드 2로 정렬하고 파일 2를 필드 1로 정렬합니다.
( head -n1 file1.csv && tail -n+2 file1.csv | sort -t, -k2,2 ) > sort1.csv
( head -n1 file2.csv && tail -n+2 file2.csv | sort -t, -k1,1 ) > sort2.csv
하이퍼 슬러그의 매개 변수 확장 :
-k 2,2 : character sort starting and stopping on 2nd field
-k 1,1 : character sort starting and stopping on 1st field
head -n1 : read first line
tail -n+1: : read all but first line
( ) : subshell
> : output to file
CSV 파일의 첫 번째 헤더 행을 유지 때 )하려면 서브 쉘 ( )
내에서 head
및 tail
을 수행해야했습니다. 주어진 필드로 정렬 .
그때,
join -t , -a1 -a2 -1 2 -2 1 -o auto sort1.csv sort2.csv > sort3.csv
하이퍼 슬러그의 매개 변수 확장 :
-t , : ',' is the field separator
-a1 : Do not omit lines from file 1 if no match in file 2 found
-a2 : Do not omit lines from file 2 if no match in file 1 found.
-1 2 : file 1, 2nd field
-2 1 : file 2, 1st field
-o auto : Auto format: includes extra commas indicating unmatched fields
> : output to file
다음은 file1.csv
, file2.csv
및 결과 sort3.csv
의 예입니다.
file1.csv
:
ID,email
02,[email protected]
03,[email protected]
05,[email protected]
07,[email protected]
11,[email protected]
file2.csv
:
email,name
[email protected],Timothy Brown
[email protected],Robert Green
[email protected],Raul Vasquez
[email protected],Carol Lindsey
sort3.csv
:
email,ID,name
[email protected],02,Robert Green
[email protected],,Carol Lindsey
[email protected],03,
[email protected],07,Raul Vasquez
[email protected],05,
[email protected],,Timothy Brown
[email protected],11,
Timothy Brown과 Carol Lindsey는 ID가 없지만 결합 된 CSV 파일에는 여전히 포함되어 있습니다 (이름과 이메일이 올바른 필드에 있음).
LibreOffice와 같은 스프레드 시트 프로그램으로 CSV 파일을 읽고 VLOOKUP()
매크로를 사용하여 두 번째 파일에서 이름을 검색 할 수 있습니다.
Go 사용 : https://github.com/chrislusf/gleam
package main
import (
"flag"
"os"
"github.com/chrislusf/gleam"
"github.com/chrislusf/gleam/source/csv"
)
var (
aFile = flag.String("a", "a.csv", "first csv file with 2 fields, the first one being the key")
bFile = flag.String("b", "b.csv", "second csv file with 2 fields, the first one being the key")
)
func main() {
flag.Parse()
f := gleam.New()
a := f.Input(csv.New(*aFile))
b := f.Input(csv.New(*bFile))
a.Join(b).Fprintf(os.Stdout, "%s,%s,%s\n").Run()
}