리눅스 grep find의 매개변수에 대한 문제

shari83의 이미지

안녕하세요.

이번에 간단한 alias를 짜보았는데 이게 도저히 결과가 제대로 나오질 않아서 질문을 남겨 봅니다.
아무리 검색해도 이것에 대한건 없는것 같더라구요

alias gr='find . -type f | xargs grep --colour=auto -Hn $0'
alias gr='grep --colour=auto -Hn $0 `find . -type f`'

일단 위 두개로 시작을 했고, 직접 명령어를 타이프 하면 작동하는걸 확인했습니다.

find . -type f | xargs grep --colour=auto -Hn string 의 경우
alias로 할때와 그냥 할때의 답이 동일 했습니다.

이것의 문제는

결과물이
765:26284: send_to_char

이런식으로 나온다는데 있습니다.

grep --colour=auto -Hn $0 `find . -type f`의 경우

alias로 만들면 작동하지 않으며. 매개변수 $0을 string 으로 직접 줬을경우

./genobj.c:356: free_object_strings(to);
./genobj.c:368: copy_object_strings(to, from);

의 제대로된 결과물이 나옵니다.

이외에 여러가지 방법을 사용해봤으나. 아랫 결과물처럼 제대로된 결과를 얻기 힘들거나 작동이 되지 않았습니다. 도움 부탁드립니다.

./genobj.c:368: copy_object_strings(to, from);

이런식의 결과물을 원합니다 ㅠ

익명 사용자의 이미지

find 내에 있는 -exec 옵션을 써보는건 어떨까요?

qiiiiiiiip의 이미지

alias는 파라미터를 받지 않습니다.

그냥 별도 스크립트로 저장하여 사용하든지,
꼭 alias를 해야겠으면 아래 링크 참고하세요.

http://stackoverflow.com/questions/7131670/make-bash-alias-that-takes-parameter

첫번째 alias의 경우에는 가능하겠네요. 대신 $0를 빼야합니다.
alias gr='find . -type f | xargs grep --colour=auto -Hn'

gr string 이라고 하면 되야할텐데요..
아마 $0 가 이상하게 동작했을 듯..

shari83의 이미지

일단 스크립트로 만들어서 해결보았구요.
첫번째는 $1을 사용해서 알리아스가 동작했습니다.
두번째는 안됐구요. 링크걸어 주신문서를 참고하면 alias로도 가능할것 같군요!

ymir의 이미지

첫번째를 $1 로 썼는데 동작한건, $1 에 아무런 값이 없었기 때문입니다.
$0, $1 는 현재 shell 이 호출될 때의 argument 가 들어 있기 때문에, 그냥 가져다 쓰시면 안됩니다.

$ echo "$0 $1 $2"
/bin/bash
$ alias fih='find /usr/include -type f -name \*.h | xargs grep --color=auto -Hn $0'
$ set -x
$ fih getpwent
+ xargs grep --color=auto -Hn /bin/bash getpwent
+ find /usr/include -type f -name '*.h'
grep: getpwent: No such file or directory
$ set +x
+ set +x
$ alias fih='find /usr/include -type f -name \*.h | xargs grep --color=auto -Hn $1'
$ set -x
$ fih getpwent
+ xargs grep --color=auto -Hn getpwent
+ find /usr/include -type f -name '*.h'
/usr/include/python2.7/pyconfig.h:329:/* Define to 1 if you have the `getpwent' function. */
/usr/include/pwd.h:85:extern struct passwd *getpwent (void);
/usr/include/pwd.h:95:extern struct passwd *fgetpwent (FILE *__stream);
/usr/include/pwd.h:129:   PLEASE NOTE: the `getpwent_r' function is not (yet) standardized.
/usr/include/pwd.h:140:extern int getpwent_r (struct passwd *__restrict __resultbuf,
/usr/include/pwd.h:164:extern int fgetpwent_r (FILE *__restrict __stream,

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

ymir의 이미지

첫번째 alias 에서 $0 만 빼면 그런대로 동작할겁니다.

$ alias fih='find /usr/include -type f -name \*.h | xargs grep --color=auto -Hn'
$ fih getpwent
/usr/include/python2.7/pyconfig.h:329:/* Define to 1 if you have the `getpwent' function. */
/usr/include/pwd.h:85:extern struct passwd *getpwent (void);
/usr/include/pwd.h:95:extern struct passwd *fgetpwent (FILE *__stream);
/usr/include/pwd.h:129:   PLEASE NOTE: the `getpwent_r' function is not (yet) standardized.
/usr/include/pwd.h:140:extern int getpwent_r (struct passwd *__restrict __resultbuf,
/usr/include/pwd.h:164:extern int fgetpwent_r (FILE *__restrict __stream,

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

shari83의 이미지

같은 -Hn 옵션을 줬는데.. 제가 쓴방식은 파일이름과 라인이 엉망진창으로 나오고.

요방법은 제대로된 결과물이 출력됐습니다.

shari83의 이미지

shari83@SSN-77 /home/shari83/test/src $ find . -exec perl -pi -e 's/기술을/기술은/g' {} \; 2>/dev/null
shari83@SSN-77 /home/shari83/test/src $ gr 기술
./act.offensive.c:128: send_to_char(ch, "그런 기술은 모릅니다.\r\n");
./act.offensive.c:276: send_to_char(ch, "그런 기술은 모릅니다.\r\n");
./act.offensive.c:331: send_to_char(ch, "그런 기술은 모릅니다.\r\n");

요런 쉘 스크립트를 작성하려고 합니다.

#!/bin/bash
find . -exec perl -pi -e 's/$1/$2/g' {} \; 2>/dev/null

안되더군요.. 아마도 '' 요것때문이나. / 이것때문에 말썽을 부리는것 같은데요. 틸트문자를 넣거나 해야 하는건가요?

var1="find . -exec perl -pi -e 's/$1/$2/g' {} \; 2>/dev/null"
echo $var1
$ cha 기술을 기술은

이라고 했을때 제대로

find . -exec perl -pi -e 's/기술을/기술은/g' {} \; 2>/dev/null

이라고 표현 되었습니다.. 다만 작동자체는 안하구요.

ymir의 이미지

소스 파일명이 왠지 낯설지가 않군요.. ㅎㅎ

근데 스크립트는 뭘 하시겠다는 건지 감이 잘 안 잡히는군요.
쉘 스크립트에서 변수를 ' (작은따옴표) 로 감싸는 경우에는 변수가 아닌 문자열 그대로 해석됩니다.
변수를 사용하려는 경우에는 " (큰따옴표) 로 바꿔 주셔야 합니다.

alias 를 사용하는 경우에는, alias 로 정의된 명령줄 뒤에 입력한 매개 변수가 그대로 paste 되어서 하나의 명령이 완성된다고 보시면 됩니다.
그래서 다음과 같은 방식으로 쓸 수 있는 거구요.

$ alias fff='find . -type f -name \*.c -o -name \*.h | xargs perl -pi -e'
$ fff 's/include/INCLUDE/g'
$ head src/main.c
#INCLUDE <stdio.h>
#INCLUDE <stdlib.h>
#INCLUDE <string.h>
...

매개변수를 활용하려면 쉘 스크립트나 function 을 쓰시면 됩니다.

$ unalias fff
$ function fff() { find . -type f -name \*.c -o -name \*.h | xargs perl -pi -e "s/$1/$2/g"; }
$ fff INCLUDE include
$ head src/main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
...

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

shari83의 이미지

' 싱글이 문제였네요..저게 굳이 필요 없는거였음에도. 쓰라고 일러준 블로그에 원망하며 ㅠㅠ

혹시 ' 이게 있을때도 $를 이스케이프 시키지 않는 그런 방법이 있나요?

qiiiiiiiip의 이미지

" 를 쓰세요..

댓글 달기

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