2011-03-22 2 views
0

다음 파일이 있다고 가정하십시오.awk에 = 분리 기호로 공백을 사용하도록 알려주는 방법 (공백을 제거함)

John=good 
Tom = ok 
Tim = excellent 

나는 다음을 알고있다 : =을 분리기로 사용하자.

awk -F= '{print $1,$2}' file 

이렇게하면 다음과 같은 결과가 나옵니다.

John good 
Tom  ok 
Tim  excellent 

공백을 무시하고 이름과 그 출력 만 인쇄하도록하겠습니다.

이 문제를 해결하는 한 가지 방법은 다른 결과로 awk입니다.

awk -F= '{print$1,$2}' file | awk '{print $1,$2}' 

그러나 나는 이것을 하나만 할 수 있는지 알고 싶었습니다. awk?

답변

4

구분 기호 정의에 포함하십시오. 그것은 정규 표현식입니다.

jinx:1654 Z$ awk -F' *= *' '{print $1, $2}' foo.ds 
John good 
Tom ok 
Tim excellent 
+0

에서

. 감사. – Russell

1

FS 변수를 정규 표현식으로 설정할 수 있습니다. 너무 빨리했다 AWK 설명서

The following table summarizes how fields are split, based on the value of FS. 

FS == " " 
    Fields are separated by runs of whitespace. Leading and trailing whitespace are ignored. This is the default. 
FS == any single character 
    Fields are separated by each occurrence of the character. Multiple successive occurrences delimit empty fields, as do leading and trailing occurrences. 
FS == regexp 
    Fields are separated by occurrences of characters that match regexp. Leading and trailing matches of regexp delimit empty fields. 
관련 문제