텍스트에서 문자열 찾는 리눅스 명령어가....
글쓴이: mabux / 작성시간: 수, 2007/01/24 - 2:30오후
안녕하세요
리눅스에서 검색은 grep 이나 sed를 사용하는것으로 알 고 있습니다.
하나의 텍스트에서 특정 문자열을 찾아서 그 아래에 항목의 값을 따로 파일로 만들고 싶습니다.
예를들면 아래와 같습니다.
[ String ] Table
1. Test
2. Debug
[ Value ] Table
1. One
2. Two
위와 같은 문자를 포함한 텍스트 파일에서
grep "[ String ] Table" text.txt > result.txt
를 하면 [ String ] Table 이부분만 result.txt에 출력이 되더군요..
제가 원하는 결과는
Test
Debug
위와 같은 결과를 얻고 싶습니다.
혹시 이럴 경우 검색하는 방법을 아시는 분 답변 부탁드리겠습니다.
Forums:


NAME? ? ? grep, egrep,
NAME grep, egrep, fgrep - print lines matching a pattern SYNOPSIS grep [options] PATTERN [FILE...] grep [options] [-e PATTERN | -f FILE] [FILE...] DESCRIPTION Grep searches the named input FILEs (or standard input if no files are named, or the file name - is given) for lines containing a match to the given PATTERN. By default, grep prints the matching lines. In addition, two variant programs egrep and fgrep are available. Egrep is the same as grep -E. Fgrep is the same as grep -F. OPTIONS -A NUM, --after-context=NUM Print NUM lines of trailing context after matching lines. Places a line containing -- between contiguous groups of matches. -a, --text Process a binary file as if it were text; this is equivalent to the --binary-files=text option. -B NUM, --before-context=NUM Print NUM lines of leading context before matching lines. Places a line containing -- between contiguous groups of matches.sed -n '/\[ String \]
sed -n '/\[ String \] Table/{
x
N
N
s/\n//
p
}' test.txt
그럼 이만 총총...[竹]
http://elflord.egloos.com
===== ===== ===== ===== =====
그럼 이만 총총...[竹]
http://elflord.egloos.com
예제에서 출력해야
예제에서 출력해야 할 값이 2줄고정이 아닌 가변일 경우에는 이방법이 낫겠군요. 넘버링까지 삭제한 버전입니다.
sed -n '/\[ String \] Table/,/\[/{
/^\[/!{
s/^[0-9]. //
/^$/!p
}
}' test.txt
그럼 이만 총총...[竹]
http://elflord.egloos.com
===== ===== ===== ===== =====
그럼 이만 총총...[竹]
http://elflord.egloos.com
많은 방법이
많은 방법이 있겠지만 딱 보고 생각난건
perl -ne '/\[ String \]/ .. /^$/ and print' file.txt
입니다.
사실 저도같은 류의 일을 많이 하는데 위 코드를 쓰지는 않고 왠만한 크기의 파일은
해쉬에다가 쭈욱 넣어놨다가 한번에 써버립니다.
댓글 달기