2012-10-07 1 views
1

여러 줄과 한 줄에 여러 단어를 사용하여 grep 명령을 수행하면 출력은 줄 대신 단어별로 배열에 저장되는 것으로 보입니다. 한 줄에 저장되도록 어떻게 변경할 수 있습니까? 이 10 선, 첫 번째 읽기를 반환하는 경우 그것은 단지 출력 "이"배열 요소 당 grep 출력의 한 줄 저장

+0

당신이 쉘 스크립트에 대해 이야기를? –

+0

@VaughnCato, 네, 죄송합니다. 예를 들어, bash 스크립트의 an_array = ('grep blah file.txt')는 전체 행이 아닌 각 요소에 단어를 넣어 출력을 저장합니다. – Sam

+0

무슨 뜻인지 증명할 수있는 코드를 제공해 주시겠습니까? –

답변

2

당신은 필드 구분 변경 IFS를 사용할 수 있습니다

IFS=' 
' 
first_title=($(egrep -o 'class\=\"title\" title\=\"'.\{1,80\} index.html 
| egrep -o title\=\".*\"\> | sed 's/title\=\"//g' | sed 's/">//g')) 


echo ${first_title[0]} 
unset IFS 
0

을 추가 할 것입니다 경우

"이 라인은"

first_title=($(egrep -o 'class\=\"title\" title\=\"'.\{1,80\} index.html 
| egrep -o title\=\".*\"\> | sed 's/title\=\"//g' | sed 's/">//g')) 


echo ${first_title[0]} 

: 예를 들어

공백이있는 요소 인 경우 다음 예제와 같이 인용해야합니다.

arr=("this is a line" "this is another line" this is some words) 
echo "${arr[0]}" 
this is a line 
printf '%s\n' "${arr[@]}" 
this is a line 
this is another line 
this 
is 
some 
words 

그래서 y 우리의 경우는, 그런 것을 시도 :

first_title=(
    $(
     egrep -o 'class\=\"title\" title\=\"'.\{1,80\} index.html | 
      egrep -o title\=\".*\"\> | 
      sed 's/title\=\"//g; 
       s/">//g; 
       s/.*/"&"/' 
    ) 
) 


echo "${first_title[0]}" 
관련 문제