셸: 다중 열 파일 처리

셸: 다중 열 파일 처리

다음 형식의 열 파일은 macOS를 사용하고 있습니다.

 ./tmp/7000_CNE_lig752_cl_0.5| -10.1000| 58
./tmp/7000_CNE_lig1029_cl_0.5| -8.0200| 40
./tmp/7000_CNE_lig183_cl_0.5| -8.0400| 32
./tmp/7000_CNE_lig247_cl_0.5| -8.3000| 28
./tmp/7000_CNE_lig331_cl_0.5| -8.9400| 31
./tmp/7000_CNE_lig578_cl_0.5| -8.2400| 76
./tmp/7000_CNE_lig587_cl_0.5| -8.5800| 35
./tmp/7000_CNE_lig659_cl_0.5| -8.0600| 31
./tmp/7000_CNE_lig679_cl_0.5| -8.7500| 36
./tmp/7000_CNE_lig767_cl_0.5| -8.9600| 86
./tmp/7000_CNE_lig830_cl_0.5| -8.2700| 32
./tmp/7000_CNE_lig841_cl_0.5| -8.1600| 32
./tmp/7000_CNE_lig842_cl_0.5| -8.5700| 33
./tmp/7000_CNE_lig900_cl_0.5| -8.1400| 61
./tmp/7000_CNE_lig931_cl_0.5| -8.9400| 40
./tmp/7000_CNE_lig933_cl_0.5| -8.6200| 101
./tmp/7000_CNE_lig987_cl_0.5| -8.6200| 55
./tmp/7000_CNE_lig996_cl_0.5| -8.2300| 31
./tmp/7000_CNE_lig998_cl_0.5| -8.6800| 48
./tmp/7000_CNE_lig999_cl_0.5| -8.0000| 29
./tmp/7000_CNE_lig1002_cl_0.5| -7.0900| 32
./tmp/7000_CNE_lig1009_cl_0.5| -7.4600| 22
./tmp/7000_CNE_lig1023_cl_0.5| -7.8700| 25
./tmp/7000_CNE_lig1032_cl_0.5| -7.1200| 20
./tmp/7000_CNE_lig1033_cl_0.5| -7.4400| 26
./tmp/7000_CNE_lig118_cl_0.5| -7.4400| 11
./tmp/7000_CNE_lig179_cl_0.5| -7.8000| 26
./tmp/7000_CNE_lig180_cl_0.5| -7.3800| 38
./tmp/7000_CNE_lig24_cl_0.5| -7.3600| 32
./tmp/7000_CNE_lig25_cl_0.5| -7.0600| 26

두 번째 열을 기준으로 값을 가장 낮은 것부터 가장 높은 것까지 정렬하는 한 줄 SORT 명령이 필요합니다. sort 명령을 사용하여 시도한 내용은 다음과 같습니다.

sort -t'|' -nk2 7000_CNE_TOP.csv
7000_CNE_lig752_cl_0.5| -10.1000| 58
7000_CNE_lig1029_cl_0.5| -8.0200| 40
7000_CNE_lig183_cl_0.5| -8.0400| 32
7000_CNE_lig247_cl_0.5| -8.3000| 28
7000_CNE_lig331_cl_0.5| -8.9400| 31
7000_CNE_lig578_cl_0.5| -8.2400| 76
7000_CNE_lig587_cl_0.5| -8.5800| 35
7000_CNE_lig659_cl_0.5| -8.0600| 31
7000_CNE_lig679_cl_0.5| -8.7500| 36
7000_CNE_lig767_cl_0.5| -8.9600| 86
7000_CNE_lig830_cl_0.5| -8.2700| 32
7000_CNE_lig841_cl_0.5| -8.1600| 32
7000_CNE_lig842_cl_0.5| -8.5700| 33
7000_CNE_lig900_cl_0.5| -8.1400| 61
7000_CNE_lig931_cl_0.5| -8.9400| 40
7000_CNE_lig933_cl_0.5| -8.6200| 101
7000_CNE_lig987_cl_0.5| -8.6200| 55
7000_CNE_lig996_cl_0.5| -8.2300| 31
7000_CNE_lig998_cl_0.5| -8.6800| 48
7000_CNE_lig999_cl_0.5| -8.0000| 29
7000_CNE_lig1002_cl_0.5| -7.0900| 32
7000_CNE_lig1009_cl_0.5| -7.4600| 22
7000_CNE_lig1023_cl_0.5| -7.8700| 25
7000_CNE_lig1032_cl_0.5| -7.1200| 20
7000_CNE_lig1033_cl_0.5| -7.4400| 26
7000_CNE_lig118_cl_0.5| -7.4400| 11
7000_CNE_lig179_cl_0.5| -7.8000| 26
7000_CNE_lig180_cl_0.5| -7.3800| 38
7000_CNE_lig24_cl_0.5| -7.3600| 32

또는 동일한 결과를 제공하는 -nk 2,2를 사용하십시오.

sort -t'|' -nk2,2 7000_CNE_TOP.csv
7000_CNE_lig752_cl_0.5| -10.1000| 58
7000_CNE_lig1029_cl_0.5| -8.0200| 40
7000_CNE_lig183_cl_0.5| -8.0400| 32
7000_CNE_lig247_cl_0.5| -8.3000| 28
7000_CNE_lig331_cl_0.5| -8.9400| 31
7000_CNE_lig578_cl_0.5| -8.2400| 76
7000_CNE_lig587_cl_0.5| -8.5800| 35
7000_CNE_lig659_cl_0.5| -8.0600| 31
7000_CNE_lig679_cl_0.5| -8.7500| 36
7000_CNE_lig767_cl_0.5| -8.9600| 86
7000_CNE_lig830_cl_0.5| -8.2700| 32
7000_CNE_lig841_cl_0.5| -8.1600| 32
7000_CNE_lig842_cl_0.5| -8.5700| 33
7000_CNE_lig900_cl_0.5| -8.1400| 61
7000_CNE_lig931_cl_0.5| -8.9400| 40
7000_CNE_lig933_cl_0.5| -8.6200| 101
7000_CNE_lig987_cl_0.5| -8.6200| 55
7000_CNE_lig996_cl_0.5| -8.2300| 31
7000_CNE_lig998_cl_0.5| -8.6800| 48
7000_CNE_lig999_cl_0.5| -8.0000| 29
7000_CNE_lig1002_cl_0.5| -7.0900| 32
7000_CNE_lig1009_cl_0.5| -7.4600| 22
7000_CNE_lig1023_cl_0.5| -7.8700| 25
7000_CNE_lig1032_cl_0.5| -7.1200| 20
7000_CNE_lig1033_cl_0.5| -7.4400| 26
7000_CNE_lig118_cl_0.5| -7.4400| 11
7000_CNE_lig179_cl_0.5| -7.8000| 26
7000_CNE_lig180_cl_0.5| -7.3800| 38

답변1

sed부동 소수점 숫자를 정렬하려면 로캘을 끈 상태에서 1을 사용하세요 .

sed 's=^\./tmp/\([^|]*\)\.txt|=\1|=' 1 | LC_ALL=C sort -t'|' -k2,2n

sed 표현식은 ./tmp/각 줄의 시작 부분에서 제거하지만 .txt첫 번째 줄 앞의 내용 도 제거합니다 |. 이것이 캐치가 비파이프라인과 일치하는 이유입니다.

-k2,2저는 두 번째 열을 기준으로 정렬 하곤 했습니다.오직. 두 번째 열의 값이 동일한 경우 를 사용하면 -k2다음 열을 기준으로 정렬됩니다(이 경우 원하는 것이 무엇인지 잘 모르겠습니다).

답변2

./tmp/.txt각각이 항상 한 줄에 한 번만 나타나는 경우 다음을 수행할 수 있습니다.

sed 's|\./tmp/||; s|\.txt||' file | sort -t'|' -nk2,2

비결은,초로바도 언급했듯이, -k2,2두 번째 필드만 정렬하는 데 사용합니다.

또는 Perl에서 모든 작업을 수행할 수 있습니다.

$ perl -F'\|' -lane 's|\./tmp/||; ## remove the .tmp
                     s|\.txt||;   ## remove the .txt
                     $k{$F[1]}=$_;  ## Use the 2nd field as the key for a hash 
                                    ## and the line as the value
                     }{  ## after the end of the file...
                    ## sort the hash's keys and print its values
                    print $k{$_} for sort {$a<=>$b} keys(%k)' file
7000_CNE_lig1036_cl_0.5| -10.1000| 58
7000_CNE_lig1029_cl_0.5| -8.0200| 40
7000_CNE_lig1023_cl_0.5| -7.8700| 25
7000_CNE_lig1009_cl_0.5| -7.4600| 22
7000_CNE_lig1033_cl_0.5| -7.4400| 26
7000_CNE_lig1032_cl_0.5| -7.1200| 20
7000_CNE_lig1002_cl_0.5| -7.0900| 32
7000_CNE_lig1035_cl_0.5| -6.4100| 22
7000_CNE_lig1027_cl_0.5| -6.2000| 21
7000_CNE_lig1016_cl_0.5| -6.0700| 22
7000_CNE_lig1024_cl_0.5| -5.9800| 35
7000_CNE_lig1019_cl_0.5| -5.9300| 19
7000_CNE_lig1034_cl_0.5| -5.8600| 21
7000_CNE_lig1020_cl_0.5| -5.8000| 18
7000_CNE_lig101_cl_0.5| -5.6200| 19
7000_CNE_lig1003_cl_0.5| -5.4600| 24
7000_CNE_lig1017_cl_0.5| -5.3300| 18
7000_CNE_lig1010_cl_0.5| -5.3000| 21
7000_CNE_lig1001_cl_0.5| -5.2900| 13
7000_CNE_lig1015_cl_0.5| -5.2300| 26
7000_CNE_lig1028_cl_0.5| -5.0700| 20
7000_CNE_lig1018_cl_0.5| -4.9500| 20
7000_CNE_lig1025_cl_0.5| -4.9100| 29
7000_CNE_lig100_cl_0.5| -4.5300| 13

관련 정보