두 번째 필드가 여러 줄에 걸쳐 있는 콜론으로 구분된 파일의 줄의 경우 끝에 있는 새 줄을 바꿉니다.

두 번째 필드가 여러 줄에 걸쳐 있는 콜론으로 구분된 파일의 줄의 경우 끝에 있는 새 줄을 바꿉니다.

서로 다른 콘텐츠 세트가 포함된 여러 파일이 있지만 서로 다른 파일의 여러 줄에 대한 패턴은 동일합니다.

입력 파일 예: cat -n test.txt

     1  adjfkhjdhfkjd: dfkjfkdljgklfjgklfjgkfjghk
     2  rtueitjrkldngfmdn: kldfjkdjgkdjgkldjgkfjhklfjkkjhkgjhklfk
     3  dkljfkdljgkldg: fgdkjgkfdljglkfdjgkldfjgkljkldfjgkljgkflj
     4  a12kjsdk232kjk445kjkjlk34323lkjkjlk3422
     5  98094kjhjkh23434hjhk32453242hkjhkjhkj
     6  bncvmcnbxmbvcnmxbvnxbcnxbnxcmbvnxc: xckdfjgklfdjgklfdjglkfdjgio
     7  iourtiourtioreutiorutoir: i3948j35hj4309457480jkh5jk450
     8  5ui43u3213435
     9  5io4p3i54op3i5op34i5po34i5
    10  54390859043860943853kj5h34jkh534jk543
    11  jkljkljkjkjkjklj: hgjjjjjjjjjjjjj
    12  4i34935kjtkrelthrket: 4539859435943hkjhkjhk34543ll4
    13  jg;lfdjglkfjlkfjghlkfd
    14  f;dgljdfl;hj;df
    15  fglkdjlkjgkldfjgklfdjhklfhjdflkj

예상 출력:

adjfkhjdhfkjd: dfkjfkdljgklfjgklfjgkfjghk
rtueitjrkldngfmdn: kldfjkdjgkdjgkldjgkfjhklfjkkjhkgjhklfk
dkljfkdljgkldg: fgdkjgkfdljglkfdjgkldfjgkljkldfjgkljgkflj a12kjsdk232kjk445kjkjlk34323lkjkjlk3422 98094kjhjkh23434hjhk32453242hkjhkjhkj
bncvmcnbxmbvcnmxbvnxbcnxbnxcmbvnxc: xckdfjgklfdjgklfdjglkfdjgio
iourtiourtioreutiorutoir: i3948j35hj4309457480jkh5jk450 5ui43u3213435 5io4p3i54op3i5op34i5po34i5 54390859043860943853kj5h34jkh534jk543
jkljkljkjkjkjklj: hgjjjjjjjjjjjjj
4i34935kjtkrelthrket: 4539859435943hkjhkjhk34543ll4 lfdjglkfjlkfjghlkfd f;dgljdfl;hj;df fglkdjlkjgkldfjgklfdjhklfhjdflkj

답변1

잘 알려진 sed 레시피의 변형을 사용할 수 있습니다.

sed -e :a -e '$!N;/\n.*:/!s/\n/ /;ta' -e 'P;D' test.txt

즉, 이전 줄이 있으면 그 줄 뒤에 줄을 추가합니다(개행 문자를 공백으로 바꿈).아니요콜론이 포함되어 있습니다. 그래서 주어진

$ cat -n test.txt
     1  adjfkhjdhfkjd: dfkjfkdljgklfjgklfjgkfjghk
     2  rtueitjrkldngfmdn: kldfjkdjgkdjgkldjgkfjhklfjkkjhkgjhklfk
     3  dkljfkdljgkldg: fgdkjgkfdljglkfdjgkldfjgkljkldfjgkljgkflj
     4  a12kjsdk232kjk445kjkjlk34323lkjkjlk3422
     5  98094kjhjkh23434hjhk32453242hkjhkjhkj
     6  bncvmcnbxmbvcnmxbvnxbcnxbnxcmbvnxc: xckdfjgklfdjgklfdjglkfdjgio
     7  iourtiourtioreutiorutoir: i3948j35hj4309457480jkh5jk450
     8  5ui43u3213435
     9  5io4p3i54op3i5op34i5po34i5
    10  54390859043860943853kj5h34jkh534jk543
    11  jkljkljkjkjkjklj: hgjjjjjjjjjjjjj
    12  4i34935kjtkrelthrket: 4539859435943hkjhkjhk34543ll4
    13  jg;lfdjglkfjlkfjghlkfd
    14  f;dgljdfl;hj;df
    15  fglkdjlkjgkldfjgklfdjhklfhjdflkj

그 다음에

$ sed -e :a -e '$!N;/\n.*:/!s/\n/ /;ta' -e 'P;D' test.txt | cat -n
     1  adjfkhjdhfkjd: dfkjfkdljgklfjgklfjgkfjghk
     2  rtueitjrkldngfmdn: kldfjkdjgkdjgkldjgkfjhklfjkkjhkgjhklfk
     3  dkljfkdljgkldg: fgdkjgkfdljglkfdjgkldfjgkljkldfjgkljgkflj a12kjsdk232kjk445kjkjlk34323lkjkjlk3422 98094kjhjkh23434hjhk32453242hkjhkjhkj
     4  bncvmcnbxmbvcnmxbvnxbcnxbnxcmbvnxc: xckdfjgklfdjgklfdjglkfdjgio
     5  iourtiourtioreutiorutoir: i3948j35hj4309457480jkh5jk450 5ui43u3213435 5io4p3i54op3i5op34i5po34i5 54390859043860943853kj5h34jkh534jk543
     6  jkljkljkjkjkjklj: hgjjjjjjjjjjjjj
     7  4i34935kjtkrelthrket: 4539859435943hkjhkjhk34543ll4 jg;lfdjglkfjlkfjghlkfd f;dgljdfl;hj;df fglkdjlkjgkldfjgklfdjhklfhjdflkj

답변2

awk를 사용하십시오.

$ awk '/:/{if (NR>1) print r; r=$0; next} {r=r OFS $0} END{print r}' file
adjfkhjdhfkjd: dfkjfkdljgklfjgklfjgkfjghk
rtueitjrkldngfmdn: kldfjkdjgkdjgkldjgkfjhklfjkkjhkgjhklfk
dkljfkdljgkldg: fgdkjgkfdljglkfdjgkldfjgkljkldfjgkljgkflj a12kjsdk232kjk445kjkjlk34323lkjkjlk3422 98094kjhjkh23434hjhk32453242hkjhkjhkj
bncvmcnbxmbvcnmxbvnxbcnxbnxcmbvnxc: xckdfjgklfdjgklfdjglkfdjgio
iourtiourtioreutiorutoir: i3948j35hj4309457480jkh5jk450 5ui43u3213435 5io4p3i54op3i5op34i5po34i5 54390859043860943853kj5h34jkh534jk543
jkljkljkjkjkjklj: hgjjjjjjjjjjjjj
4i34935kjtkrelthrket: 4539859435943hkjhkjhk34543ll4 jg;lfdjglkfjlkfjghlkfd f;dgljdfl;hj;df fglkdjlkjgkldfjgklfdjhklfhjdflkj

답변3

사용 awk:

$ awk '{printf "%s", (($1 ~ /:$/) ? ((NR==1) ? "" : ORS) : " ")$0 } END{print ""}' file
# Or
$ awk -v ORS=' ' '{NR!=1 && gsub(/[[:alnum:]]+:/, "\n&")}1; END{printf "\n"}' file | sed 's/ $//'

@EdMorton이 제안한 대로:

입력에 printf 형식 문자(예: )가 포함된 경우 후자가 실패하고 ORS의 값을 사용하는 전자보다 낫고 ORS에 원하는 하드 코딩된 값을 사용하는 후자가 있으므로 항상 printf "%s", $n대신 수행하십시오 .printf $n%sprint ""printf "\n"

관련 정보