C shell 스크립트를 짰습니다.. 어디가 틀렸는지 데이터값이 출력이 생각대로 안나옵니다.

mewestside의 이미지

C shell 스크립트를 짰습니다..

어디가 틀렸는지 데이터값이 출력이 생각대로 안나옵니다.

제가 생각하는 시나리오는

원하는 데이터에서 파일 내용을 받아서 1컬럼을 뽑아와서

1컬럼을 1라인에 5개의 데이터가 들어가게 만드는 것입니다.

데이터파일은 다음의 형식입니다.

0.0000 -0.0000 
0.0500 0.1679 
0.1000 0.3069 
0.1500 0.3913 
0.2000 0.4016 
0.2500 0.3265 
0.3000 0.1642 
0.3500 -0.0767 
0.4000 -0.3787 
0.4500 -0.7161 
0.5000 -1.0584 
0.5500 -1.3725 
0.6000 -1.6269 
0.6500 -1.7941 
0.7000 -1.8538 
0.7500 -1.7952 
0.8000 -1.6179 
0.8500 -1.3321 
0.9000 -0.9579 
0.9500 -0.5234 
1.0000 -0.0620 
1.0500 0.3906 
1.1000 0.8003 
1.1500 1.1370 
1.2000 1.3781 
1.2500 1.5102 
1.3000 1.5304 
1.3500 1.4462 
1.4000 1.2746 
1.4500 1.0404 
1.5000 0.7729 
1.5500 0.5036 
1.6000 0.2624

제가 짠 스크립트는

#!/bin/csh
 
set file = `cat original.dat | awk '{if(NR==1) printf("%10.4lf ",$1); else if(NR%5==1) printf("%10.4lf ",$1);else if(NR%5==0) printf(" %10.4lf\n" ,$1);else printf("%10.4lf ",$1);}'`
 
echo $file > test.dat

이렇게 짰는데..

제가 생각하는 결과는

0.0000 0.0500 0.1000 0.1500 0.2000
0.2500 0.3000 0.3500 0.4000 0.4500
0.5000

이런식의 결과인데..

다음줄로 안넘어가는군요...

원래 적용이 안되는건가요??

chanik의 이미지

결과물을 변수에 저장하면서 newline이 space로 변환되기 때문인것 같군요.
csh에서 변수에 newline을 저장할 수 있는 방법은 모릅니다만,
아래와 같이 바로 파일로 저장하시면 원하시는 결과가 나올 것입니다.

#!/bin/csh
 
cat original.dat | awk '{if(NR==1) printf("%10.4lf ",$1); else if(NR%5==1) printf("%10.4lf ",$1);else if(NR%5==0) printf(" %10.4lf\n" ,$1);else printf("%10.4lf ",$1);}' > test.dat

또는 csh 대신 bash를 쓰시면 변수에 newline을 보존할 수 있습니다.
(주의: echo "$file" 의 큰따옴표를 빼먹으면 안 됨)

#!/bin/bash
 
file=`cat original.dat | awk '{if(NR==1) printf("%10.4lf ",$1); else if(NR%5==1) printf("%10.4lf ",$1);else if(NR%5==0) printf(" %10.4lf\n" ,$1);else printf("%10.4lf ",$1);}'`
echo "$file" > test.dat
mewestside의 이미지

csh 방법으로 해결했습니다...

bash 방법으로는 안되는군요...

chanik의 이미지

bash 스크립트도 테스트해보고 올린 것입니다.
혹시 csh 프롬프트에서 source 명령으로 bash 스크립트를 실행하신 것 아닌지요?

스크립트를 test.bash 에 저장했다면,
csh 프롬프트에서 아래와 같이 하거나

$ chmod 755 test.bash
$ ./test.bash

아래와 같이 하면 잘 동작할 것입니다.

$ bash test.bash

mewestside의 이미지

감사합니다..

제가 아직 많은것을 알지 못하여 생긴 문제였습니다..

새로운 방법을 알고 많이 배웠습니다..

감사합니다..

댓글 달기

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