
다음 텍스트가 있습니다.
add gmt version date
comment - text1
text1 continue
comment - text2
text2 continue
comment - text 3
text3 continue
"주석"이 처음 나타나는 부분부터 파일 끝까지의 모든 텍스트를 원합니다. 나는 sed 's/^.*Comment - //g'
다음 텍스트만 얻습니다.
text 3
text3 continue
즉, 마지막으로 나타나는 "주석"부터 파일 끝까지입니다. 하지만 "주석"이 처음 나타나는 부분부터 텍스트 끝까지의 모든 텍스트가 필요합니다.
text1
text1 continue
comment - text2
text2 continue
comment - text 3
text3 continue
답변1
가장 쉬운 방법은 sed
명령별로 그룹화하는 것입니다.
{ sed '/PATTERN/!d;s///;q'; cat; } <infile
일치하지 않는 모든 줄을 제거 d
하고 첫 번째 일치 항목에서 s
교체를 수행하고 요청에 따라 종료(자동으로 인쇄)한 다음 cat
나머지 줄(있는 경우)을 이어받아 인쇄합니다. 이 일을 혼자서
할 수 없다는 것은 아닙니다 sed
:
sed '/PATTERN/,$!d # delete all lines not in this range
//{ # if the line matches
x;//!{ # exchange and IF what was in hold space doesn't match
g;s///;b # get hold space content, replace, go to end of script
}
x # ELSE exchange back (do nothing)
}' <infile
나는 PATTERN
그것을 단순하게 유지하기 위해 사용합니다( ^.*comment -
또는 다른 패턴으로 대체).
답변2
배관을 했으나 상태가 좋지 않음
$ sed -n '/comment/,$ p' file | sed -r '0,/comment/ s/comment - (.*)/\1/'
text1
text1 continue
comment - text2
text2 continue
comment - text 3
text3 continue
설명하다
sed -n '/comment/,$ p' file
comment
처음부터 끝까지 줄을 인쇄하세요 .sed -r '0,/comment/ s/comment - (.*)/\1/'
첫 번째 줄을 찾아comment
편집하여 제거하세요.comment -