awk를 사용하여 열에서 특정 코드를 추출하는 방법은 무엇입니까?

awk를 사용하여 열에서 특정 코드를 추출하는 방법은 무엇입니까?

final.txt다음과 같은 이름의 텍스트 파일이 있습니다.

name_00000001   name_000001 -   u   q1:MSTRG.4|MSTRG.4.1|3|0.000000|0.000000|0.000000|3211
name_00000002   name_000001 -   u   q1:MSTRG.4|MSTRG.4.2|2|0.000000|0.000000|0.000000|894
name_00000003   name_000001 -   p   q1:MSTRG.4|MSTRG.4.3|2|0.000000|0.000000|0.000000|522
name_00000004   name_000002 -   p   q1:MSTRG.26|MSTRG.26.1|1|0.000000|0.000000|0.000000|336
name_00000005   name_000003 -   u   q1:MSTRG.27|MSTRG.27.1|5|0.000000|0.000000|0.000000|730
name_00000006   name_000003 -   k   q1:MSTRG.27|MSTRG.27.2|7|0.000000|0.000000|0.000000|3157
name_00000007   name_000003 -   k   q1:MSTRG.27|MSTRG.27.3|6|0.000000|0.000000|0.000000|3665
name_00000008   name_000003 -   u   q1:MSTRG.27|MSTRG.27.4|4|0.000000|0.000000|0.000000|7900
name_00000009   name_000003 -   u   q1:MSTRG.27|MSTRG.27.5|4|0.000000|0.000000|0.000000|4356
name_00000010   name_000003 -   k   q1:MSTRG.27|MSTRG.27.6|4|0.000000|0.000000|0.000000|1842
name_00000011   name_000003 -   u   q1:MSTRG.27|MSTRG.27.7|3|0.000000|0.000000|0.000000|2752
name_00000012   name_000003 -   p   q1:MSTRG.27|MSTRG.27.8|2|0.000000|0.000000|0.000000|300
name_00000013   name_000003 -   u   q1:MSTRG.27|MSTRG.27.9|2|0.000000|0.000000|0.000000|2895
name_00000014   name_000003 -   k   q1:MSTRG.27|MSTRG.27.10|2|0.000000|0.000000|0.000000|696
name_00000015   name_000003 -   u   q1:MSTRG.27|MSTRG.27.11|4|0.000000|0.000000|0.000000|9046
name_00000016   name_000003 -   u   q1:MSTRG.27|MSTRG.27.12|5|0.000000|0.000000|0.000000|9962
name_00000017   name_000003 -   u   q1:MSTRG.27|MSTRG.27.13|3|0.000000|0.000000|0.000000|17753
name_00000018   name_000003 -   l   q1:MSTRG.27|MSTRG.27.14|2|0.000000|0.000000|0.000000|6895
name_00000019   name_000003 -   l   q1:MSTRG.27|MSTRG.27.15|4|0.000000|0.000000|0.000000|1889
name_00000020   name_000003 -   l   q1:MSTRG.27|MSTRG.27.16|4|0.000000|0.000000|0.000000|4712
name_00000021   name_000003 -   u   q1:MSTRG.27|MSTRG.27.17|3|0.000000|0.000000|0.000000|1154
name_00000022   name_000003 -   u   q1:MSTRG.27|MSTRG.27.18|2|0.000000|0.000000|0.000000|511
name_00000023   name_000003 -   x   q1:MSTRG.27|MSTRG.27.19|3|0.000000|0.000000|0.000000|2984
name_00000024   name_000003 -   u   q1:MSTRG.27|MSTRG.27.20|2|0.000000|0.000000|0.000000|4944
name_00000025   name_000003 -   x   q1:MSTRG.32|MSTRG.32.1|1|0.000000|0.000000|0.000000|279
name_00000026   name_000003 -   x   q1:MSTRG.33|MSTRG.33.1|2|0.000000|0.000000|0.000000|543
name_00000027   name_000003 -   u   q1:MSTRG.34|MSTRG.34.1|2|0.000000|0.000000|0.000000|664
name_00000028   name_000003 -   u   q1:MSTRG.35|MSTRG.35.1|1|0.000000|0.000000|0.000000|3875
name_00000029   name_000003 -   o   q1:MSTRG.36|MSTRG.36.1|2|0.000000|0.000000|0.000000|969
name_00000030   name_000003 -   o   q1:MSTRG.27|MSTRG.27.21|2|0.000000|0.000000|0.000000|5750
name_00000031   name_000004 -   t   q1:MSTRG.27|MSTRG.27.22|3|0.000000|0.000000|0.000000|3425
name_00000032   name_000005 -   t   q1:MSTRG.27|MSTRG.27.24|3|0.000000|0.000000|0.000000|3403
name_00000033   name_000006 -   o   q1:MSTRG.27|MSTRG.27.23|3|0.000000|0.000000|0.000000|921
name_00000034   name_000007 -   u   q1:MSTRG.38|MSTRG.38.1|2|0.000000|0.000000|0.000000|222

네 번째 열에는 다른 코드가 있습니다(예: ). u, p, k, l, x, o, t따라서 이 특정 열에서는 유사한 코드를 추출하고 싶습니다 u, o, t, x, p.

다음과 같이 네 번째 열의 코드 중 하나를 사용하여 모든 행을 추출하려고 합니다.

cat final.txt | awk '$4=="u"{print $0}' > new.txt

동일한 명령에서 다른 코드를 추출하려면 어떻게 해야 합니까?

답변1

정규식을 사용하여 이 필드와 일치시킬 수 있습니다.

awk '$4 ~ /^[uotxp]$/' final.txt > new.txt

기본 작업은 현재 레코드를 인쇄하므로 를 작성할 필요가 없습니다 { print $0 }.

관련 정보