파일 내 문자열의 부분 고유 - 사용 가능한 첫 번째 문자열만 유지

파일 내 문자열의 부분 고유 - 사용 가능한 첫 번째 문자열만 유지

my_file.txt다음 문자열을 포함하는 파일이 있습니다 .

tasmax_day_ACCESS_historical_r1i1p1f3_gn.nc
tasmax_day_EC-Earth3_historical_r1i1p1f1_gn.nc
tasmax_day_EC-Earth3_historical_r1i1p1f1_gr.nc
tasmax_day_EC-Earth3_historical_r1i1p1f3_gn.nc
tasmax_day_HadGEM-MM_historical_r1i1p1f1_gn.nc
tasmax_day_HadGEM-MM_historical_r1i1p1f1_gr.nc
tasmax_day_HadGEM-MM_historical_r3i1p1f1_gn.nc
tasmax_day_MIROC_historical_r1i1p1f1_gn.nc
tasmax_day_MIROC_historical_r2i1p1f1_gn.nc

end unique로 시작하는 하위 문자열을 수행해야 합니다. 각 하위 문자열에 대해 알파벳 순서로 먼저 나오는 해당 줄만 유지해야 합니다.tasmax_historical

내 예상 결과는 my_file.txt다음과 같습니다.

tasmax_day_ACCESS_historical_r1i1p1f3_gn.nc
tasmax_day_EC-Earth3_historical_r1i1p1f1_gn.nc
tasmax_day_HadGEM-MM_historical_r1i1p1f1_gn.nc
tasmax_day_MIROC_historical_r1i1p1f1_gn.nc

도움을 주셔서 감사합니다.

답변1

간단한 Awk이면 충분합니다. 고유 식별자 문자열로 키가 지정된 해시 맵을 형성하고 해당 행만 인쇄합니다.

awk -F_ '{ key = $1 FS $2 FS $3 $4 } !unique[key]++ ' file

구분 기호를 로 설정한 상태에서 표기법 _으로 개별 작품에 접근 $1하고 를 포함하여 키를 구성합니다 $4. 표현식은 !unique[key]++라인의 키(형성된)가 다음과 같은 경우에만 0이 아닙니다.~ 아니다벌써 시청하였습니다.

이것가정하다귀하의 tasmax문자열은 at $1historicalat 에 발생하며 $4그렇지 않으면 작동하지 않습니다.


또는 도구를 사용하여 필드 사용 을 구분하여 sort고유한( ) 줄을 요청합니다 . BSD 및 GNU 변형 모두에서 작동합니다.-u_1-4sort

sort -u -t_ -k1,4 < file

관련 정보