첫행을 연속적으로 써 줄 수 있는 명령어가 있을까요?

riotkun의 이미지

첫행에 나오는 문자를 공백이 나올 때까지 첫열에 넣을 수 있을까요
test.txt 파일 안에

COALNE
(Coastline)
42-0-5.196600N,130-0-0.000000E
42-0-4.898520N,129-59-59.635680E
42-0-4.832640N,129-59-59.503560E
42-0-4.832640N,129-59-59.238600E
42-0-4.898520N,129-59-58.808400E
42-0-4.932000N,129-59-58.510680E

SLCONS
(Shoreline Construction)
42-0-5.196600N,130-0-0.000000E
42-0-4.898520N,129-59-59.635680E
42-0-4.832640N,129-59-59.503560E

공백 다음에 나오는 첫행을 $1에 연속적으로 넣어주는었으면 하는 작업입니다
ex)
COALNE
COALNE(Coastline)
COALNE42-0-5.196600N,130-0-0.000000E
COALNE42-0-4.898520N,129-59-59.635680E
COALNE42-0-4.832640N,129-59-59.503560E
COALNE42-0-4.832640N,129-59-59.238600E
COALNE42-0-4.898520N,129-59-58.808400E
COALNE42-0-4.932000N,129-59-58.510680E

SLCONS
SLCONS(Shoreline Construction)
SLCONS42-0-5.196600N,130-0-0.000000E
SLCONS42-0-4.898520N,129-59-59.635680E
SLCONS42-0-4.832640N,129-59-59.503560E

이런 식으로 awk를 이용해서 할 수 있을 까요?
for문을 이용해도 되고 다른 방법이 있다면 알려주세요

백연구원의 이미지

다른 방법을 언급하셨기에... 에디터에서 하시면 바로 될 텐데요.

vim ex) https://stackoverflow.com/questions/1174274/how-can-i-prepend-text-in-the-middle-of-the-line-to-multiple-lines-in-vim


소곤소곤

 의 이미지

별로 우아하지는 않은 방식이지만 파이썬으로 어떻게 해 볼 수 있겠네요.

import sys
 
prefix = None
 
for line in sys.stdin:
    s_line = line.strip()
    if not prefix:
        prefix = s_line
    elif not s_line:
        prefix = None
    else:
        s_line = prefix + s_line
    print s_line

woonuk의 이미지

awk 'NR==1 { X=$0; print X }; NR > 1 { print X $0 }' a.txt
ttt의 이미지

파일 첫번째 단어가 아니라 공백이 나올때마다 다음에 나오는 첫단어를 계속 추가하는 것이군요.
역시 우아하진 않지만 동작은 하는것 같네요.
test.txt를 읽어 result.txt로 내보내는 파이썬 코드입니다.

prefix = ''
fin    = open('test.txt', 'r')
fout   = open('result.txt', 'w')
 
for line in fin.readlines():
    if prefix == '':
        prefix = line.split('\n')[0]
        fout.write(line)
        continue
 
    if line == '\n':
        prefix = ''
 
    fout.write(prefix+line)
 
fin.close()
fout.close()
woonuk의 이미지

awk 'NR==1 { X=$0; print X; getline }; { if (!NF) { print ; getline; X=$0; print X } else { print X $0 }}' a.txt