[Shell Script] 어떻게 풀어야 할지 감이 안오네요

helperchoi의 이미지

cat test
111
222
333
444
555
666

cat test.list
222
333

[ Script 내용 ]

cat test.sh
 
#!/bin/bash
 
export LANG=C
export LC_ALL=C
 
RUNCNT=`cat ./test | wc -l`
 
for ((RUN=1; RUN <= RUNCNT ; RUN++));
do
           PRINT_LINE=`sed -n ${RUN}p ./test`    
           WORKING_LIST=`cat ./test.list`
 
           for LIST in ${WORKING_LIST}
           do
                     MODIFI_LINE_NO=`grep -n "^${LIST}" ./test | cut -d ":" -f1`     
 
                     if [ ${RUN} -eq ${MODIFI_LINE_NO} ]
                     then
                                echo "MMM - `sed -n ${RUN}p ./test`"
                     fi
           done   
                     echo ${PRINT_LINE}
done

[ 실행 결과 값 ]

sh ./test.sh
111
MMM - 222
222
MMM - 333
333
444
555
666

[ 원하는 결과값 ]

111
MMM - 222
MMM - 333
444
555
666

위 원하는 결과 값 처럼 뽑고 싶은데 어디를 어떻게 수정해야 할지 도저히 감이 안오네요;;
오늘 하루종일 삽질을........
도와주세요 ㅠㅠ

ymir의 이미지

$ cat test.txt
111
222
333
444
555
666
$ cat test.list
222
333
$ while read -r line; do grep -q $line test.list && echo -n "MMM - "; echo $line; done < test.txt
111
MMM - 222
MMM - 333
444
555
666

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

helperchoi의 이미지

RUNCNT=`cat test | wc -l`
 
for ((RUN=1; RUN <= RUNCNT ; RUN++));
do
	MODIFY_CHECK=0
	PRINT_LINE=`sed -n ${RUN}p test`
	WORKING_LIST=`cat test.list`
 
	for LIST in ${WORKING_LIST}
	do
		CHECK_MODIFY=`echo "${PRINT_LINE}" | grep "^${LIST}" | wc -l`
 
		if [ ${CHECK_MODIFY} -eq 1 ]
		then
			export MODIFY_CHECK=1
		fi
	done
 
	if [ ${MODIFY_CHECK} -eq 0 ]
	then
		echo "${PRINT_LINE}" 
	else
		echo "MMM - ${PRINT_LINE}"
	fi
done

이제는 대세다!

pynoos의 이미지

동작은 할 것 같습니다만, test, test.list를 읽어 들이는 회수는 ymir님의 코드에 비해서 비경제적입니다. ^^
스크립트는 최대한 동일한 파일 입력은 줄이는 것이 좋습니다.

익명 사용자의 이미지

comm -2 test test.list | sed 's,^\t,MMM - ,'

댓글 달기

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