[완료] 파이프 2개를 사용하는 방법은 없을까요?

sayhappy의 이미지

-------------------------------
$cat a.dat
a 1
b 2
c 3
$cat b.dat
e 4
f 5
g 6
-------------------------------
두 파일의 특정 열에 있는 숫자의 합을 구하려고 합니다.
위에 있는 a.dat와 b.dat의 두번째 열에 있는 데이터의 각각의 행에 대한 합을 구하려고 합니다.

$cat a.dat | cut -f2
1
2
3
$cat b.dat | cut -f2
4
5
6

하면 두 열의 숫자들을 얻어올 수 있는데,
이걸 awk를 이용해서 더 할 수 있는 방법이 없네요. 어떻게 깔끔하게 할 수 있는 방법 없을까요?

이런 경우엔 어쩔 수 없이 첫번째 결과를 임시적으로 파일에 쓰고 다시 읽어서 더하는 식으로 해야 하나요?
$cat a.dat | cut -f2 > tmp
$cat b.dat | cut -f2 | paste - tmp
$awk'{print $1+$2}' tmp

질문을 고쳤습니다. ^^;

slc1의 이미지

질문을 잘 이해했는지 모르겠군요..

cat a.dat|cut -f2
cat b.dat|cut -f2
결과에 대해서 뭔가 하는거 같네요..

아래와 같은 명령을 쓸수도 있습니다.
(cat a.dat;cat b.dat)| awk '{ S2+=$2; S4+=$4 } END { print "S2", S2, "S4",S4 }'

이런것도 있습니다.
특정 row만 가져와서.. 더하는
(awk 'NR==2 {print $0}' a.dat;awk 'NR==2 { print $0}' b.dat ) |
awk '{ S2+=$2; S4+=$4 } END { print "S2", S2, "S4",S4 } '

아니면 getline을 써도 될 것 같네요.

slc1의 이미지

요렇게 하면 되네요.

(cat a.dat;cat b.dat)| awk '{S2+=$2} END {print S2}'

slc1의 이미지

$cat a.dat | cut -f2 > tmp
$cat b.dat | cut -f2 | paste - tmp
$awk'{print $1+$2}' tmp

각 파일 같은 line의 두번째 필드를 더하는거 같은데..

요렇게 한번 해보세요..

paste a.dat b.dat | awk '{ print $2+$4 } '

워낙 다양한 방법이 있어놔서..

Prentice의 이미지

수정: 어이쿠 질문을 잘못 이해했습니다.

댓글 달기

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