sed에서 특정 문자 앞의 문자 제외

sed에서 특정 문자 앞의 문자 제외

한 줄만 인쇄하는 파일이 있습니다. 이 줄을 조작하기 위해 다른 sed 명령을 사용하는 데 어려움을 겪고 있습니다.

apple orange.5678 dog cat 009 you

나는 "orange.5678"을 잡고 "you"를 포함하고 다른 모든 것을 무시하고 싶습니다. 아래처럼 생겼으면 좋겠어요

orange.5678 you

어디서부터 시작해야 할지, "orange.5678"과 "you"를 제외한 모든 항목을 제외하는 방법을 모르겠습니다. 어떤 도움이라도 좋을 것입니다!

답변1

$ sed -r 's/.* ([^ ]+\.[^ ]+).* ([^ ]+)$/\1 \2/' orange
orange.5678 you

설명하다

  • -r확장 정규식 사용
  • s/old/newold사용. . . 교체new
  • .*임의의 문자 수
  • (some characters)some characters나중에 교체할 ​​때 참고할 수 있도록 저장해 두세요 .
  • [^ ]+공백이 아닌 일부 문자
  • \.텍스트 포인트
  • $줄 끝
  • \1저장된 스키마에 대한 역참조

s/.* ([^ ]+\.[^ ]+).* ([^ ]+)$/\1 \2/, 공백이 아닌 문자 앞의 공백까지 줄의 모든 항목을 일치시키고 .그 뒤에 공백이 아닌 문자를 일치시킨 다음(해당 문자를 양쪽에 저장 .), 임의의 문자를 일치시키고 마지막 줄 세트를 저장하지 않음을 의미합니다. -공백 문자를 켜고 전체 일치 항목을 공백으로 구분된 두 개의 저장된 패턴으로 바꿉니다.

답변2

가장 간단한 방법:

awk '{print $2, $6}' file.txt

실제 사용 사례가 질문이 나타내는 것보다 더 복잡하고 추가 논리가 필요한 경우(예: 그렇지 않은 경우)언제나필요한 두 번째 및 여섯 번째 필드)질문을 수정하세요밝히다.

답변3

사람들은 @Zanna의 다른 답변을 살펴봐야 합니다. 매우 우아하고 정규식의 힘을 보여줍니다.

이 표현을 사용해 보세요 gawk. 일반 awk는 그룹화와 함께 작동하지 않습니다.

^(?:\w+\s){0,}(\w+\.\w+)(?:\s\w+){0,}\s(\w+)$

다음과 같은 변경 사항으로 작동합니다.

apple orange.5678 dog cat 009 you
apple apple grape.9991 pig cat piegon owl
grape.9991 pig cat piegon owl

다음은 표현에 대한 설명입니다.

/
^(?:\w+\s){0,}(\w+\.\w+)(?:\s\w+){0,}\s(\w+)$
/
g
^ asserts position at start of the string

Non-capturing group (?:\w+\s){0,}
{0,} Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\w+ matches any word character (equal to [a-zA-Z0-9_])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\s matches any whitespace character (equal to [\r\n\t\f\v ])

1st Capturing Group (\w+\.\w+)
\w+ matches any word character (equal to [a-zA-Z0-9_])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\. matches the character . literally (case sensitive)
\w+ matches any word character (equal to [a-zA-Z0-9_])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)

Non-capturing group (?:\s\w+){0,}
{0,} Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\s matches any whitespace character (equal to [\r\n\t\f\v ])
\w+ matches any word character (equal to [a-zA-Z0-9_])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\s matches any whitespace character (equal to [\r\n\t\f\v ])

2nd Capturing Group (\w+)
\w+ matches any word character (equal to [a-zA-Z0-9_])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)

답변4

sed에 정규식을 사용해야 하는 경우 위의 답변으로 해결할 수 있습니다. 대안이 열려 있는 경우:

gv@debian: $ read -r a b c d e f<<<"apple orange.5678 dog cat 009 you" && echo "$b $f" 
orange.5678 you

이것이 파일의 한 줄인 경우 <<<"...."다음으로 바꾸십시오.<file

이 방법이 작동하려면 기본 IFS=space가 필요합니다. 중복되는 경우 IFS=" "처음부터 적용하십시오.

관련 정보