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

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

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.