[완료]2개의연결파일에서 sed 나 awk를 이용하여....도움요청입니다.

shwezarl의 이미지

제가 코딩을 할줄몰라...리눅스 sed나 awk를 많이써서 이쪽으로 문의드립니다.

우선 서로 연관된 파일이 두개가 있습니다.

a라는 파일은
1:4010:0:0:0:
2:4010:0:0:0:
.
.
이런식으로 맨앞은 순서대로 넘버링된 걸로 나뉘어져있구여

b라는 파일은 a의 순서대로 매칭되어

1:44:58185633:15256987:58185421:15256898:58185069:15256946:58184769:15256952:58184508 ...
2:403:58190082:15283199:58190082:15283198:58190095:15283179:58190098:15283172 ...
.
.
b파일의 두번째 필드(첫번째줄 44)는 그 뒷부분의 58185633:15256987(좌표점) 이것을 하나로 44개가 있다는 뜻으로 쭉 되어있습니다...

여기 b파일에서 두번째 필드가 3이하 즉 좌표점이 3개이하를 지우고 다시 새로 넘버링을 하고싶습니다..

이때 a파일은 b파일에서 지워지는 줄과 동일하게 지워져서 이것또한 새로 빠진거빼고 넘버링이 되어야 싱크가

맞아야합니다.

그래서 제가 머리좀굴려서 해봤으나..b파일쪽만...

awk ' FS=":" { if (2 < $2) printf($0);print"\n" }' [파일명] > 새로받을[파일명]

이것또한 첫번째줄이 무조건 지워집니다..

잘안됩니다...도움좀..

bushi의 이미지

복잡하지 않게, 간단히 커맨드 두어개로 전처리를 해봤습니다.

[bushi@rose txt]$ cat a.txt
1:4010:0:0:0:
2:4010:0:0:0:
3:4010:0:0:0:
[bushi@rose txt]$ cat b.txt
1:44:58185633:15256987:58185421:15256898:58185069:...
2:2:58190082:15283199:58190082:15283198
3:403:58190082:15283199:58190082:15283198:58190095:...
[bushi@rose txt]$
[bushi@rose txt]$ 
[bushi@rose txt]$ sed -r "s|^(.*):[012]:(.*)|\1\:DELETE_THIS|" b.txt > _b.txt
[bushi@rose txt]$ paste a.txt _b.txt > _a.txt
[bushi@rose txt]$ grep -v "DELETE_THIS" _a.txt | cut -f1 > a_new.txt
[bushi@rose txt]$ grep -v "DELETE_THIS" _b.txt > b_new.txt
[bushi@rose txt]$ cat a_new.txt
1:4010:0:0:0:
3:4010:0:0:0:
[bushi@rose txt]$ cat b_new.txt
1:44:58185633:15256987:58185421:15256898:58185069:...
3:403:58190082:15283199:58190082:15283198:58190095:...
[bushi@rose txt]$

gawk 를 이용해서 첫번째 컬럼의 숫자를 다듬으면 될 것 같네요.

OTL

shwezarl의 이미지

sed식 중에...sed -r "s|^(.*):[012]:(.*)|\1\:DELETE_THIS|" b.txt > _b.txt

3이하라서...[012]를 주신것인지..5이하 라면 [01234]이렇게 넣는것인지..궁금하고요..

sed 정규식 ^ , (.*) [0-9*]이런 거 관련해서 좀더 자세한설명 나와있는 곳 좀 아시면

알려주세요~ 참유용한거같은데...쓸려면 이런것들을 잘몰라서...간단한부분밖에 사용을 못해

제한적인게 많습니다..ㅠㅠ

송효진의 이미지

http://kr2.php.net/manual/en/pcre.pattern.php
php 매뉴얼이지만 pcre 자체에 대해 설명되어 있는 부분이므로,
읽기에 괜찮을듯 합니다.
sed 는 웬만큼 pcre (perl 과 호환되는 정규식) 와 호환됩니다.

http://phpschool.com/gnuboard4/bbs/board.php?bo_table=tipntech&sca=&sfl=wr_subject&stx=pcre&sop=and&x=0&y=0
이것도 *-_-*

emerge money
http://wiki.kldp.org/wiki.php/GentooInstallSimple - 명령어도 몇개 안되요~
http://xenosi.de/

aero의 이미지

Perl로 하면
a.txt

1:4010:0:0:0:
2:4010:0:0:0:
3:4010:0:0:0:

b.txt

1:44:58185633:15256987:58185421:15256898:58185069:...
2:2:58190082:15283199:58190082:15283198
3:403:58190082:15283199:58190082:15283198:58190095:...

Perl코드

#!/usr/bin/perl
use strict;
use warnings;
 
open my $a, '<', 'a.txt';
open my $b, '<', 'b.txt';
open my $ao, '>', 'aout.txt';
open my $bo, '>', 'bout.txt';
 
while (<$b>) {
    my $line = <$a>;
    if ( (split /:/,$_)[1] > 3) {
        print {$bo} $_;
        print {$ao} $line;
    }
}

코드는 간단합니다. "b라는 파일은 a의 순서대로 매칭되어" 라고 했으므로
두 파일을 동시에 열어서 둘 다 한 줄씩 읽고 b파일이 특정조건을 만족할 때만
출력파일쪽으로 읽은 줄을 찍어주면 되겠죠.

<결과> aout.txt

1:4010:0:0:0:
3:4010:0:0:0:
bout.txt
1:44:58185633:15256987:58185421:15256898:58185069:...
3:403:58190082:15283199:58190082:15283198:58190095:...

댓글 달기

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