정리가 필요한 텍스트 파일이 많이 있습니다. Im UNIX bash를 사용하므로 AWK 또는 grep이 좋습니다.
다음과 같은 텍스트 파일 :
1766 1789
1764 1790
1762 1849
0
1357 1817
1366 1857
0
360 42
352 95
0
293 142
302 181
delete-this
0
302 181
0
내가 원하는 것은 "0", "delete-this"가있는 모든 행을 삭제하는 것입니다. 두 개의 열이있는 한 행 또는 두 개의 열이있는 세 개의 행만 삭제합니다.
결과는 다음과 같습니다.
1766 1789
1762 1849
1357 1817
1366 1857
360 42
352 95
293 142
302 181
감사합니다!
추가 정보 : 1 행 2 열과 2 행 2 열의 합계는 1보다 커야합니다. 그렇지 않은 경우 2 행을 삭제해야합니다.
이것은 어렵거나 이해하기 어려웠지만 다시 시작합니다.
awk '/[0-9]+ [0-9]+/ {a[++t]=$0;b[t]=$2;next} {if (t>=2) for (i=1;i<=t;i++) {if (b[i]-c!=1) print a[i];c=b[i]};t=0}'
1766 1789
1762 1849
1357 1817
1366 1857
360 42
352 95
293 142
302 181
작동 원리 :
awk '
/[0-9]+ [0-9]+/ { # if line does have 2 column of number, then
a[++t]=$0 # add line to array "a" and increment variable "t"
b[t]=$2 # add column 2 to array "b"
next # go to next line
}
{
if (t>=2) # is there more two or more lines with numbers connrected, then
for (i=1;i<=t;i++) { # loop trough array "a" with all numbers
if (b[i]-c!=1) # test if the difference between this number in column 2 is more than 1 compare to previous line
print a[i] # then print array "a"
c=b[i] # store array "b" information in variable "b"
}
;t=0 # clear counter "t"
}' file
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다