perl 질문 하나 올립니다.

jip102의 이미지

안녕하세요.
아래와 같은 텍스트 파일(test.txt)을 가공하려 하는데 감이 안잡히네여

텍스트 파일의 내용은 아래와 같습니다.

select * from tab union all
select * from tab union all
select * from tab union all

제가 하고자 하는건 위의 2줄은 union all이 있어야 하고
마지막 3번째 라인(맨끝의 select 절)의 union all 만 ";" 로 교체하고자 하는데 쉽지가 않네여
line의 eof를 어떻게 가공할 방법이 없을까요?

마지막 라인(3번째)의 끝에 있는 특정 문자만 바꾸는게 가능하나요?

jip102의 이미지

select * from tab union all
select * from tab union all
select * from tab union all

--> 아래와 같이 변경되고자 합니다.

select * from tab union all
select * from tab union all
select * from tab;

chanik의 이미지

"perl one liner" 검색해서 해 봤습니다. 사용자가 많은지 자료가 참 많이 나오네요.
아래 기록은 실행기록이 맞기는 한데, #주석은 제가 편집해 넣은 것이니까 감안해서 보십시오.

$ cat test2.txt
select * from tab1 union all
select * from tab2 union all
select * from tab3 union all
 
select * from tab4 union all
select * from tab5 union all
select * from tab6 union all
#
# 마지막 줄에 있는 union all만 ;로 치환
#
$ cat test2.txt | perl -ne 'print $last; $last = $_; END { $last =~ s/\s*union all/;/i; print $last}'
select * from tab1 union all
select * from tab2 union all
select * from tab3 union all
 
select * from tab4 union all
select * from tab5 union all
select * from tab6;
#
# 공백문자로만 이뤄진 라인이 나올 때마다 그 전 라인의 union all을 ;로 마무리해줌
#
$ cat test2.txt | perl -ne '$last =~ s/\s*union all/;/i if /^\s*$/; print $last; $last = $_; END { $last =~ s/\s*union all/;/i; print $last}'
select * from tab1 union all
select * from tab2 union all
select * from tab3;
 
select * from tab4 union all
select * from tab5 union all
select * from tab6;
jip102의 이미지

이런 기법이 있었다니 잘참고하겠습니다.
감사합니다.

chanik의 이미지

http://kldp.org/node/140697#comment-600190 에 대한 답글을 이곳에 달아둡니다.
내용상 이곳에 달아두어야 다른 분들께도 참고가 될 것 같습니다.

Quote:

위의 부분을 File Open 문을 이용하여 할 수는 없을까요?
제가 테스트를 해본결과 if문을 써서 union all 매칭되는 키워드를 ';' 로 바꾸기는 하는데 전부 바껴 버려서..(아직 내공이 부족하군요)
Open 문을 사용하여 마지막 라인만 ';' 바꾸는게 가능할까요?

아래와 같이 하면 되지 않을까요? test.txt를 읽어들여서 마지막줄만 문자열치환하고 표준출력으로 내보냅니다. 사실 저도 perl 초보자이니 제 코드 너무 믿지마시고 적당히 참고해서 고쳐 쓰십시오..

#!/usr/bin/env perl
 
use strict;
use warnings;
 
my @lines;
 
open my $fh, '<', 'test.txt';
chomp(@lines = <$fh>);
 
$lines[-1] =~ s/\s*union all/;/i;
 
print $_, "\n" for @lines;

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.