sed를 사용하여 텍스트 파일의 줄을 처리하여 샘플 문서를 생성하는 스크립트를 작성하려고합니다. 대부분의 스크립트가 작동하지만 가장자리 케이스가 하나 붙어 있습니다. 다음 파일을 고려하십시오.
line-1
line-2, part2
line-3-should-be-a-very-long,
line-3-continued
line-4
문제는 전부는 아니지만 일부 줄이 특수 토큰으로 끝납니다 (쉼표로 나타남). 토큰은 하나의 긴 줄을 생성하기 위해 줄을 다음 줄과 연결해야 함을 나타냅니다.
따라서 내 예 line-3-should-be-a-very-long,
에서는와 연결하여 line-3-continued
나에게 제공 line-3-should-be-a-very-long, line-3-continued
해야합니다 (쉼표를 유지하고 싶습니다). 행 끝에없는 쉼표가 포함되어 있더라도 행 2에는 특별한 조치가 없습니다.
처리의 나머지 부분은 몇 배관에 의해 수행 sed
과 grep
sed를 솔루션이 잘 맞는 것, 그래서 함께 명령을.
$ sed '/,$/{N;s/\n//;}' file
line-1
line-2
line-3-should-be-a-very-long, line-3-continued
line-4
공백을 삭제해야하는 경우 :
$ sed '/,$/{N;s/\n[[:blank:]]*//;}' file
line-1
line-2
line-3-should-be-a-very-long,line-3-continued
line-4
(줄 사이에 공백을 하나만 남기 //
려면 코드에서로 대체하십시오 / /
)
다음과 같이 행을 여러 번 계속할 수있는 경우
line-1
line-2
line-3-should-be-a-very-long,
line-3-continued,
line-3-continued-further
line-4
그때,
$ sed '/,$/{:loop;N;s/\n[[:blank:]]*//;/,$/bloop;}' file
line-1
line-2
line-3-should-be-a-very-long,line-3-continued,line-3-continued-further
line-4
이 마지막 sed
스크립트는 주석으로 설명되었습니다.
/,$/{ # if the current line ends with a comma, then...
:loop # define label "loop"
N # append next line from input (a newline will be inserted in-between)
s/\n[[:blank:]]*// # delete that newline and any blanks (tabs or spaces) directly after it
/,$/bloop # if the line now ends with comma, branch to the "loop" label
}
# implicit output of (possibly) modified line at end
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다