토큰으로 구분된 Sed 병합 라인

토큰으로 구분된 Sed 병합 라인

샘플 문서를 생성하기 위해 sed를 사용하여 텍스트 파일의 줄을 처리하는 스크립트를 작성하려고 합니다. 대부분의 스크립트가 작동하지만 극단적인 상황에 직면했습니다. 다음 문서를 고려하십시오

line-1
line-2, part2
line-3-should-be-a-very-long,
    line-3-continued
line-4

문제는 일부(전부는 아님) 행이 특수 토큰(쉼표로 나타남)으로 끝난다는 것입니다. 이 플래그는 하나의 긴 줄을 생성하기 위해 이 줄을 다음 줄과 연결해야 함을 나타냅니다.

따라서 내 예에서는 to가 나에게 line-3-should-be-a-very-long,연결되어야 합니다 (쉼표를 유지하고 싶습니다). 2번째 줄에는 줄 끝이 아닌 쉼표가 포함되어 있지만 특별한 작업이 없습니다.line-3-continuedline-3-should-be-a-very-long, line-3-continued

나머지 처리는 일부 명령 sed과 명령을 함께 grep파이프하여 수행되므로 sed 솔루션이 완벽하게 맞습니다.

답변1

$ 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

답변2

sed '/,$/{N;s/\n[[:blank:]]\+/ /}' file

쉼표로 끝나는 줄이 보이면 다음 줄을 읽고 다음 줄의 줄 바꿈과 선행 공백을 단일 공백으로 바꿉니다.

관련 정보