쉘 프로그래밍 중에서 구분자로 문자를 잘라내기

fox9의 이미지

쉘 프로그래밍에서
aaa-bbb-cccc-ddd
eee-ffff-ggg

등의 입력을 받아서

aaa-bbb-cccc
eee-fff

등 구분자 중에서 맨 마지막에 붙은 부분만 잘라낸 앞부분을 얻고 싶습니다.
쉘프로그래밍을 잘 모르다 보니 무엇을 어찌할지를 모르겠네요.
혹시나 방법을 아시는 분 계시면 부탁드립니다.

그럼..

nohmad의 이미지

ruby를 쓸 수 있다면 아주 간단합니다.

$ echo "a-b-c" | ruby -e 'puts $<.read.split("-")[0...-1].join("-")'
a-b

풀이하자면, 입력스트림($<, $stdin, STDIN)을 읽어서(read) '-'로 쪼개고(split), 마지막 요소만 뺀 배열([0...-1])을 '-'로 다시 합친(join) 것입니다.

yui의 이미지

bash라면

yui@Alpha:~/tmp
[1014 0] $ cat test.sh
kaka="aaa-bbb-ccc"
echo ${kaka%-*}
yui@Alpha:~/tmp
[1015 0] $ ./test.sh
aaa-bbb

@ bash를 쓰시려나 모르겠네요. ^^;;

cinsk의 이미지

Yet Another Bash Script. :)

#!/bin/sh
                                                                                
while read line; do
    echo "$line" | sed -e 's/-[^-]*$//g'
done

$ ./tmp/sh<RET>
aaa-bbb-ccc-ddd<RET>
aaa-bbb-ccc
eee-fff-ggg<RET>
eee-fff
$ _

$ cat tmp.txt | tmp.sh <<EOF <RET>
> aaa-bbb-ccc-ddd<RET>
> eee-fff-ggg<RET>
EOF<RET>
aaa-bbb-ccc
eee-fff
$ _
nonots의 이미지

#!/bin/sh

for KK in `cat source.txt`
do
echo ${KK%-*}
done

와 같이 하면 되겠고.. 파이프로 데이타를 받으려면..

[root@nalle bin]# cat a.sh
#!/bin/sh

for KK in `cat -`
do
echo ${KK%-*}
done

와 같이 한후

cat source.txt | a.sh

와 같이 하면 될듯...


=== 건달의 경지를 꿈꾸며 ===

FlOw의 이미지

Python

$ cat > ./test.py
#!/usr/bin/python
import string
import sys
for line in  sys.stdin.readlines():
        print string.join(string.split(line,"-")[0:-1],"-")

$ cat source.txt | ./test.py

-------------------- 절취선 --
행복하세요:)

댓글 달기

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