bash shell로 문자열 치환 코멘트 부탁드립니다.

snowavalanch의 이미지

bash shell로 문자열을 치환하려고 합니다.

아래와 같은 txt가 있는데,

=========================================================
130409.14:26:47
1 5000 aaa
=========================================================
130409.14:27:47
2 6000 bbb
=========================================================
130409.14:28:47
3 7000 ccc
=========================================================
130409.14:29:47
4 8000 ddd
=========================================================
130409.14:30:47
5 9000 fff
=========================================================
130409.14:31:47
6 10000 ggg

요렇게 바꾸려고 합니다.

130409.14:26:47 5000
130409.14:27:47 6000
130409.14:28:47 7000
130409.14:29:47 8000
130409.14:30:47 9000

시간줄 하고 두번째 줄을 합치고, cut 명령어 쓰면 될 듯 한데,
sed로 시간줄 하고 두번째 줄을 합치는 것이 안되네요.
bash shell script로 하려고 합니다.

ymir의 이미지

$ cat in.txt
=========================================================
130409.14:26:47
1 5000 aaa
=========================================================
130409.14:27:47
2 6000 bbb
=========================================================
130409.14:28:47
3 7000 ccc
=========================================================
130409.14:29:47
4 8000 ddd
=========================================================
130409.14:30:47
5 9000 fff
=========================================================
130409.14:31:47
6 10000 ggg
$ cat in.txt | grep -v '^=' | while read -r line; do set -a $line; [ $# -eq 1 ] && echo -n "$1 " || echo $2; done
130409.14:26:47 5000
130409.14:27:47 6000
130409.14:28:47 7000
130409.14:29:47 8000
130409.14:30:47 9000
130409.14:31:47 10000
$ cat in.txt | grep -v '^=' | sed -e '2~2 s/.* \([0-9]*\) .*/\1/g' | paste - -
130409.14:26:47 5000
130409.14:27:47 6000
130409.14:28:47 7000
130409.14:29:47 8000
130409.14:30:47 9000
130409.14:31:47 10000
$ cat in.txt | grep -v '^=' | sed -e 'N;s/\n/ /g' | cut -d' ' -f1,3
130409.14:26:47 5000
130409.14:27:47 6000
130409.14:28:47 7000
130409.14:29:47 8000
130409.14:30:47 9000
130409.14:31:47 10000

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

woonuk의 이미지

cat input.txt | awk '{ if ( NR % 3 == 2 ) { printf "%s", $1 } else if ( NR % 3 == 0 ) { printf " %s\n", $2 } }'
snowavalanch의 이미지

=========================================================
130409.14:26:47
1 5000 aaa
=========================================================
130409.14:27:47
2 6000 bbb
=========================================================
130409.14:28:47
3 7000 ccc
=========================================================
130409.14:29:47
4 8000 ddd
=========================================================
130409.14:30:47
=========================================================
130409.14:31:47
6 10000 ggg

요렇게
130409.14:30:47
의 경우는
5 9000 fff가 없는 경우인데, 이때 0을 넣으려고 합니다.

130409.14:26:47 5000
130409.14:27:47 6000
130409.14:28:47 7000
130409.14:29:47 8000
130409.14:30:47 0
130409.14:31:47 10000

과 같이 찍으려고 합니다.

| while read -r line; do set -a $line; [ $# -eq 1 ] && echo -n "$1 " || echo $2; done
여기서 "|| echo $2" 에 조건을 걸어서 0을 찍도록 해야할 것같은데, 조건 걸 수가 있는지요...

snowavalanch의 이미지

좀 문제가 있네요.

cat in.txt | grep -v '^=' | awk '{ if (intf "\n%s 0", $1 } else if ( NF == 3 ) { printf "%s", $2 }}'

ymir의 이미지

$ cat in.txt
=========================================================
130409.14:26:47
1 5000 aaa
=========================================================
130409.14:27:47
2 6000 bbb
=========================================================
130409.14:28:47
3 7000 ccc
=========================================================
130409.14:29:47
4 8000 ddd
=========================================================
130409.14:30:47
=========================================================
130409.14:31:47
6 10000 ggg
$ cat in.txt | sed -e ':a;N;$!ba;s/\n/ /g' -e 's/ =/\n=/g' | while read -r line; do set -a $line; V=${4-0}; echo "$2 $V"; done
130409.14:26:47 5000
130409.14:27:47 6000
130409.14:28:47 7000
130409.14:29:47 8000
130409.14:30:47 0
130409.14:31:47 10000
 
$ cat in2.txt
=========================================================
130409.14:26:47
1 5000 aaa
=========================================================
130409.14:27:47
2 6000 bbb
=========================================================
130409.14:28:47
3 7000 ccc
=========================================================
130409.14:29:47
4 8000 ddd
=========================================================
130409.14:30:47
=========================================================
130409.14:31:47
6 10000 ggg
=========================================================
$ cat in2.txt | while read -r line; do if [[ $line = ==* ]] ; then [ -n "$DT" ] && echo "$DT $V"; V=0; else set -a $line; [ $# -eq 1 ] && DT=$1 || V=${2-0}; fi; done
130409.14:26:47 5000
130409.14:27:47 6000
130409.14:28:47 7000
130409.14:29:47 8000
130409.14:30:47 0
130409.14:31:47 10000

두번째 거는 입력 데이터 마지막에 레코드 구분자 ========== 추가.

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

oosap의 이미지

여기있는 내용들 잘 공부해봐야겠어요.. 무척 유용할 것 같습니다. 감사합니다.

Thanks for being one of those who care for people and mankind.
I'd like to be one of those as well.

woonuk의 이미지

위에 답은 나와 있지만, awk 참고용으로 댓글 달아둡니다 ^^

cat input.txt | grep -v ^== | awk '{ if ( NF == 1 && PNF == 1) { PNF = 0; printf "0\n%s ", $1 } else if ( NF == 1 ) { PNF = NF; printf "%s ", $1 } else { PNF = NF; printf "%s\n", $2 } }'
snowavalanch의 이미지

일일히 댓글 달아주신 분들께 감사드립니다.
많은 도움이 되네요.

이런거 답 달아 주시는 분은 어떤 분야 일하시는지 궁금하기도 합니다. ^^;

댓글 달기

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