특정 값에 도달하는 행의 값을 선택하는 방법

특정 값에 도달하는 행의 값을 선택하는 방법

다음과 같은 파일에 값이 있습니다.

"AERH1","505549_AdelaideCBDWest_3@505549_AdelaideCBDWest_3",BTS3900,16,16,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,
"AERH1","558018_ElizabethVale_3@558018_ElizabethVale_3_UL",BTS3900,448,336,6,1,1,90,0,1,1,1,1,1,0,6,0,6,6,6,6,0,6,0,0,0,0,0,6,6,0,0,0,6,6,0,6,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,15,0,0,0,0,0,0,1,0,0,0,0,0,0,6,0,6,0,6,0,0,0,0,0,0,0,0,0,0,0,0,
"AERH1","505138_Netley_3@505138_Netley_3",BTS3900,448,336,6,1,1,90,0,1,1,1,1,1,0,6,0,6,6,6,6,0,6,0,0,0,0,0,6,6,0,0,0,6,6,6,6,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,12,0,1,0,0,0,0,1,0,1,0,0,0,0,6,0,6,0,6,0,0,0,0,0,0,0,0,0,6,0,0,
"AERH1","558012_OneTreeHill_2@558012_OneTreeHill_2_UL",BTS3900,544,400,9,1,1,135,0,1,1,1,1,1,1,9,0,9,9,9,9,0,9,0,0,0,0,0,9,9,0,0,0,9,9,12,9,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,30,0,1,0,0,0,0,1,0,3,0,0,0,0,9,0,9,0,9,0,0,0,0,0,0,0,0,0,0,0,0,

아래와 같이 각 줄에 최대 6개의 쉼표를 선택해야 합니다.

"AERH1","505549_AdelaideCBDWest_3@505549_AdelaideCBDWest_3",BTS3900,16,16,1

답변1

사용 cut:

cut -d, -f 1-6 file

또는 awk:

awk -F, '{OFS=","; print $1,$2,$3,$4,$5,$6}' file

두 경우 모두 출력은 다음과 같습니다.

"AERH1","505549_AdelaideCBDWest_3@505549_AdelaideCBDWest_3",BTS3900,16,16,1
"AERH1","558018_ElizabethVale_3@558018_ElizabethVale_3_UL",BTS3900,448,336,6
"AERH1","505138_Netley_3@505138_Netley_3",BTS3900,448,336,6
"AERH1","558012_OneTreeHill_2@558012_OneTreeHill_2_UL",BTS3900,544,400,9

답변2

한 가지 옵션은 awk를 사용하는 것입니다.

awk -F, '{ for(i=1;i<=5;i++) { printf "%s,",$i } printf "%s\n",$6 }' filename

쉼표로 구분된 각 필드를 최대 5번 찾아보고 각 필드 뒤에 쉼표를 입력한 후 마지막으로 여섯 번째 섹션을 인쇄합니다.

답변3

CSV에는 일부 불쾌한 경우가 있으므로 CSV 파서를 사용하는 것이 좋습니다. 예를 들어 Ruby의 경우:

ruby -rcsv -e 'CSV.foreach(ARGV.shift) {|row| puts CSV.generate_line(row.first(6))}' file

관련 정보